package main import ( "context" "log" "strings" "time" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) type MongoStore struct { client *mongo.Client db *mongo.Database usersCol *mongo.Collection ownersCol *mongo.Collection filesCol *mongo.Collection sharesCol *mongo.Collection dbName string } type mongoOwnerDoc struct { ID string `bson:"_id"` Path string `bson:"path"` Owners []string `bson:"owners"` UpdatedAt string `bson:"updated_at"` } type mongoFileMetaDoc struct { ID string `bson:"_id"` Path string `bson:"path"` Owner string `bson:"owner"` Size int64 `bson:"size"` UpdatedAt string `bson:"updated_at"` } func NewMongoStore(uri, dbName string) (*MongoStore, error) { uri = strings.TrimSpace(uri) if uri == "" { return nil, nil } if dbName == "" { dbName = "upload_thtuanit" } ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() clientOpts := options.Client(). ApplyURI(uri). SetServerSelectionTimeout(10 * time.Second). SetMaxPoolSize(20) client, err := mongo.Connect(ctx, clientOpts) if err != nil { return nil, err } if err := client.Ping(ctx, nil); err != nil { _ = client.Disconnect(context.Background()) return nil, err } db := client.Database(dbName) ms := &MongoStore{ client: client, db: db, usersCol: db.Collection("users"), ownersCol: db.Collection("file_owners"), filesCol: db.Collection("files_metadata"), sharesCol: db.Collection("shares"), dbName: dbName, } log.Printf("Connected to MongoDB Atlas successfully (database=%s)", dbName) return ms, nil } // --- Users Collection Operations --- func (m *MongoStore) LoadAllUsers() ([]UserAccount, error) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() cur, err := m.usersCol.Find(ctx, bson.M{}) if err != nil { return nil, err } defer cur.Close(ctx) var out []UserAccount for cur.Next(ctx) { var u UserAccount if err := cur.Decode(&u); err == nil && u.Username != "" { out = append(out, u) } } return out, cur.Err() } func (m *MongoStore) UpsertUserDoc(u UserAccount) error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() key := strings.ToLower(strings.TrimSpace(u.Username)) doc := bson.M{ "_id": key, "username": u.Username, "display_name": u.DisplayName, "role": u.Role, "password_salt": u.PasswordSalt, "password_hash": u.PasswordHash, "permissions": bson.M{ "can_upload": u.Permissions.CanUpload, "can_delete": u.Permissions.CanDelete, "can_rename": u.Permissions.CanRename, }, "active": u.Active, "created_at": u.CreatedAt, } _, err := m.usersCol.UpdateOne( ctx, bson.M{"_id": key}, bson.M{"$set": doc}, options.Update().SetUpsert(true), ) return err } func (m *MongoStore) DeleteUserDoc(username string) error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() key := strings.ToLower(strings.TrimSpace(username)) _, err := m.usersCol.DeleteOne(ctx, bson.M{"_id": key}) return err } // --- File Owners Collection Operations --- func (m *MongoStore) LoadAllOwners() (map[string][]string, error) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() cur, err := m.ownersCol.Find(ctx, bson.M{}) if err != nil { return nil, err } defer cur.Close(ctx) res := make(map[string][]string) for cur.Next(ctx) { var doc mongoOwnerDoc if err := cur.Decode(&doc); err == nil && doc.Path != "" && len(doc.Owners) > 0 { res[doc.Path] = doc.Owners } } return res, cur.Err() } func (m *MongoStore) UpsertOwnerDoc(remotePath string, owners []string) error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() now := time.Now().Format("2006-01-02 15:04:05") doc := mongoOwnerDoc{ ID: remotePath, Path: remotePath, Owners: owners, UpdatedAt: now, } _, err := m.ownersCol.UpdateOne( ctx, bson.M{"_id": remotePath}, bson.M{"$set": doc}, options.Update().SetUpsert(true), ) return err } func (m *MongoStore) DeleteOwnerDoc(remotePath string) error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() _, err := m.ownersCol.DeleteOne(ctx, bson.M{"_id": remotePath}) _, _ = m.filesCol.DeleteOne(ctx, bson.M{"_id": remotePath}) return err } func (m *MongoStore) UpsertFileMeta(remotePath, owner string, size int64) { ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second) defer cancel() doc := mongoFileMetaDoc{ ID: remotePath, Path: remotePath, Owner: strings.ToLower(strings.TrimSpace(owner)), Size: size, UpdatedAt: time.Now().Format("2006-01-02 15:04:05"), } _, _ = m.filesCol.UpdateOne( ctx, bson.M{"_id": remotePath}, bson.M{"$set": doc}, options.Update().SetUpsert(true), ) } // --- Shares Collection Operations --- func (m *MongoStore) LoadAllShares() ([]ShareLink, error) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() cur, err := m.sharesCol.Find(ctx, bson.M{}) if err != nil { return nil, err } defer cur.Close(ctx) var out []ShareLink for cur.Next(ctx) { var sh ShareLink if err := cur.Decode(&sh); err == nil && sh.ID != "" && sh.Path != "" { out = append(out, sh) } } return out, cur.Err() } func (m *MongoStore) UpsertShareDoc(sh ShareLink) error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() _, err := m.sharesCol.UpdateOne( ctx, bson.M{"_id": sh.ID}, bson.M{"$set": sh}, options.Update().SetUpsert(true), ) return err } func (m *MongoStore) DeleteShareDoc(shareID string) error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() _, err := m.sharesCol.DeleteOne(ctx, bson.M{"_id": shareID}) return err }