Mohammad Shahid commited on
Commit
2fa5701
·
1 Parent(s): 946a017

some major updates

Browse files
internal/commands/start.go CHANGED
@@ -5,7 +5,9 @@ package commands
5
  import (
6
  "EverythingSuckz/fsb/config"
7
  "EverythingSuckz/fsb/internal/utils"
 
8
  "math/rand"
 
9
 
10
  "github.com/celestix/gotgproto/dispatcher"
11
  "github.com/celestix/gotgproto/dispatcher/handlers"
@@ -47,6 +49,19 @@ func start(ctx *ext.Context, u *ext.Update) error {
47
  return dispatcher.EndGroups
48
  }
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  welcomeText := "Hi! I am the **File Stream Bot**.\n\nSend me any file and I will give you a direct, streamable link. Use the buttons below to get started or learn more."
51
  markup := getStartKeyboard()
52
  ctx.Reply(u, welcomeText, &ext.ReplyOpts{Markup: markup})
 
5
  import (
6
  "EverythingSuckz/fsb/config"
7
  "EverythingSuckz/fsb/internal/utils"
8
+ "fmt" // Import fmt
9
  "math/rand"
10
+ "strings" // Import strings
11
 
12
  "github.com/celestix/gotgproto/dispatcher"
13
  "github.com/celestix/gotgproto/dispatcher/handlers"
 
49
  return dispatcher.EndGroups
50
  }
51
 
52
+ args := strings.Split(u.EffectiveMessage.GetMessage(), " ")
53
+ if len(args) > 1 && args[1] == "connect" {
54
+ userID := u.EffectiveUser().ID
55
+ message := fmt.Sprintf(
56
+ "Welcome! You're connecting your account.\n\n" +
57
+ "Here is your unique Telegram User ID. Copy this ID and paste it on the website to complete the process.\n\n" +
58
+ "Your ID: `%d`", // Using backticks for easy copy-paste
59
+ userID,
60
+ )
61
+ ctx.Reply(u, message, nil)
62
+ return dispatcher.EndGroups // Stop further processing
63
+ }
64
+
65
  welcomeText := "Hi! I am the **File Stream Bot**.\n\nSend me any file and I will give you a direct, streamable link. Use the buttons below to get started or learn more."
66
  markup := getStartKeyboard()
67
  ctx.Reply(u, welcomeText, &ext.ReplyOpts{Markup: markup})
internal/db/database.go CHANGED
@@ -16,6 +16,7 @@ var (
16
  Links *mongo.Collection
17
  Users *mongo.Collection
18
  OTPs *mongo.Collection
 
19
  )
20
 
21
  // InitDatabase initializes the MongoDB connection.
@@ -46,6 +47,7 @@ func InitDatabase(log *zap.Logger) error {
46
  Links = database.Collection("links")
47
  Users = database.Collection("users")
48
  OTPs = database.Collection("otps")
 
49
 
50
 
51
  log.Info("MongoDB connection established successfully.")
 
16
  Links *mongo.Collection
17
  Users *mongo.Collection
18
  OTPs *mongo.Collection
19
+ Files *mongo.Collection
20
  )
21
 
22
  // InitDatabase initializes the MongoDB connection.
 
47
  Links = database.Collection("links")
48
  Users = database.Collection("users")
49
  OTPs = database.Collection("otps")
50
+ Files = database.Collection("files")
51
 
52
 
53
  log.Info("MongoDB connection established successfully.")
internal/db/models.go CHANGED
@@ -35,4 +35,25 @@ type OTPRequest struct {
35
  OTPHash string `bson:"otp_hash"`
36
  ExpiresAt time.Time `bson:"expires_at"`
37
  CreatedAt time.Time `bson:"created_at"`
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  }
 
35
  OTPHash string `bson:"otp_hash"`
36
  ExpiresAt time.Time `bson:"expires_at"`
37
  CreatedAt time.Time `bson:"created_at"`
38
+ }
39
+
40
+ // UserRecord represents a user from the 'users' collection.
41
+ // We only need the fields Go needs to interact with.
42
+ type UserRecord struct {
43
+ ID primitive.ObjectID `bson:"_id,omitempty"`
44
+ TelegramID int64 `bson:"telegram_id"`
45
+ }
46
+
47
+ type FileRecord struct {
48
+ ID primitive.ObjectID `bson:"_id,omitempty"`
49
+ UserID primitive.ObjectID `bson:"userId"`
50
+ FolderID *primitive.ObjectID `bson:"folderId,omitempty"` // Pointer to allow null
51
+ MessageID int `bson:"messageId"`
52
+ FileName string `bson:"fileName"`
53
+ FileSize int64 `bson:"fileSize"`
54
+ MimeType string `bson:"mimeType"`
55
+ ShortHash string `bson:"shortHash"`
56
+ FullHash string `bson:"fullHash"`
57
+ CreatedAt time.Time `bson:"createdAt"`
58
+ UpdatedAt time.Time `bson:"updatedAt"`
59
  }
internal/utils/helpers.go CHANGED
@@ -3,19 +3,21 @@ package utils
3
  import (
4
  "EverythingSuckz/fsb/config"
5
  "EverythingSuckz/fsb/internal/cache"
6
- "EverythingSuckz/fsb/internal/types"
7
  "EverythingSuckz/fsb/internal/db"
8
- "time"
9
  "context"
10
  "errors"
11
  "fmt"
12
  "math/rand"
 
13
 
14
  "github.com/celestix/gotgproto"
15
  "github.com/celestix/gotgproto/ext"
16
  "github.com/celestix/gotgproto/storage"
17
  "github.com/gotd/td/tg"
18
  "go.uber.org/zap"
 
 
19
  )
20
 
21
  // https://stackoverflow.com/a/70802740/15807350
@@ -221,12 +223,14 @@ func RefreshFileReference(ctx context.Context, client *gotgproto.Client, message
221
 
222
 
223
 
 
 
224
  // GenerateStreamLink takes a message context and a source message ID, processes it,
225
  // and returns the generated streamable link and the file properties.
226
  func GenerateStreamLink(ctx *ext.Context, u *ext.Update, messageID int) (string, *types.File, error) {
227
  log := Logger.Named("GenerateStreamLink")
228
 
229
- // 1. Forward the message to the log channel
230
  chatId := u.EffectiveChat().GetID()
231
  update, err := ForwardMessages(ctx, chatId, config.ValueOf.LogChannelID, messageID)
232
  if err != nil {
@@ -234,7 +238,7 @@ func GenerateStreamLink(ctx *ext.Context, u *ext.Update, messageID int) (string,
234
  return "", nil, fmt.Errorf("could not forward message: %w", err)
235
  }
236
 
237
- // 2. Extract file properties from the forwarded message
238
  var newMsg *tg.Message
239
  var newMsgID int
240
  for _, up := range update.Updates {
@@ -257,26 +261,52 @@ func GenerateStreamLink(ctx *ext.Context, u *ext.Update, messageID int) (string,
257
  return "", nil, fmt.Errorf("could not get file data: %w", err)
258
  }
259
 
260
- // 3. Generate the secure link
261
  fullHash := PackFile(file.FileName, file.FileSize, file.MimeType, file.ID)
262
  hash := GetShortHash(fullHash)
263
  link := fmt.Sprintf("%s/stream/%d?hash=%s", config.ValueOf.Host, newMsgID, hash)
264
 
265
- if db.Links != nil {
266
- trackedLink := db.TrackedLink{
267
- MessageID: newMsgID,
268
- UserID: u.EffectiveUser().ID,
269
- ShortHash: hash,
270
- FullHash: fullHash,
271
- LastRefreshedAt: time.Now(),
272
- CreatedAt: time.Now(),
273
- }
274
- _, err := db.Links.InsertOne(context.Background(), trackedLink)
275
- if err != nil {
276
- log.Error("Failed to save tracked link to MongoDB", zap.Error(err))
277
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
278
  }
279
 
 
280
  log.Info("Successfully generated link", zap.String("link", link))
281
  return link, file, nil
282
- }
 
3
  import (
4
  "EverythingSuckz/fsb/config"
5
  "EverythingSuckz/fsb/internal/cache"
 
6
  "EverythingSuckz/fsb/internal/db"
7
+ "EverythingSuckz/fsb/internal/types"
8
  "context"
9
  "errors"
10
  "fmt"
11
  "math/rand"
12
+ "time"
13
 
14
  "github.com/celestix/gotgproto"
15
  "github.com/celestix/gotgproto/ext"
16
  "github.com/celestix/gotgproto/storage"
17
  "github.com/gotd/td/tg"
18
  "go.uber.org/zap"
19
+
20
+ "go.mongodb.org/mongo-driver/bson"
21
  )
22
 
23
  // https://stackoverflow.com/a/70802740/15807350
 
223
 
224
 
225
 
226
+ // GenerateStreamLink takes a message context and a source message ID, processes it,
227
+ // and returns the generated streamable link and the file properties.
228
  // GenerateStreamLink takes a message context and a source message ID, processes it,
229
  // and returns the generated streamable link and the file properties.
230
  func GenerateStreamLink(ctx *ext.Context, u *ext.Update, messageID int) (string, *types.File, error) {
231
  log := Logger.Named("GenerateStreamLink")
232
 
233
+ // 1. Forward the message to the log channel (no change here)
234
  chatId := u.EffectiveChat().GetID()
235
  update, err := ForwardMessages(ctx, chatId, config.ValueOf.LogChannelID, messageID)
236
  if err != nil {
 
238
  return "", nil, fmt.Errorf("could not forward message: %w", err)
239
  }
240
 
241
+ // 2. Extract file properties from the forwarded message (no change here)
242
  var newMsg *tg.Message
243
  var newMsgID int
244
  for _, up := range update.Updates {
 
261
  return "", nil, fmt.Errorf("could not get file data: %w", err)
262
  }
263
 
264
+ // 3. Generate the secure link (no change here)
265
  fullHash := PackFile(file.FileName, file.FileSize, file.MimeType, file.ID)
266
  hash := GetShortHash(fullHash)
267
  link := fmt.Sprintf("%s/stream/%d?hash=%s", config.ValueOf.Host, newMsgID, hash)
268
 
269
+ // *** NEW LOGIC: SAVE THE FILE RECORD FOR THE DASHBOARD ***
270
+ if db.Files != nil && db.Users != nil {
271
+ go func() {
272
+ dbCtx := context.Background()
273
+
274
+ // a. Find the user in the 'users' collection by their telegram_id
275
+ var userRecord db.UserRecord
276
+ err := db.Users.FindOne(dbCtx, bson.M{"telegram_id": u.EffectiveUser().ID}).Decode(&userRecord)
277
+ if err != nil {
278
+ // User hasn't logged into the website yet. Don't save the file to the dashboard.
279
+ // We can just log this and continue.
280
+ log.Warn("User not found in dashboard users collection. File will not appear on dashboard.", zap.Int64("telegramID", u.EffectiveUser().ID))
281
+ return
282
+ }
283
+
284
+ // b. Create the new file record
285
+ now := time.Now()
286
+ fileRecord := db.FileRecord{
287
+ UserID: userRecord.ID,
288
+ FolderID: nil, // Uploads to root by default
289
+ MessageID: newMsgID,
290
+ FileName: file.FileName,
291
+ FileSize: file.FileSize,
292
+ MimeType: file.MimeType,
293
+ ShortHash: hash,
294
+ FullHash: fullHash,
295
+ CreatedAt: now,
296
+ UpdatedAt: now,
297
+ }
298
+
299
+ // c. Insert the record into the 'files' collection
300
+ _, err = db.Files.InsertOne(dbCtx, fileRecord)
301
+ if err != nil {
302
+ log.Error("Failed to save file record to MongoDB for dashboard", zap.Error(err))
303
+ } else {
304
+ log.Info("Successfully saved file to dashboard.", zap.String("fileName", file.FileName), zap.String("userID", userRecord.ID.Hex()))
305
+ }
306
+ }()
307
  }
308
 
309
+
310
  log.Info("Successfully generated link", zap.String("link", link))
311
  return link, file, nil
312
+ }