You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.3 KiB
71 lines
2.3 KiB
11 months ago
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||
|
//
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
|
||
12 months ago
|
package mgo
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
6 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
|
||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
|
||
12 months ago
|
|
||
7 months ago
|
"github.com/openimsdk/tools/db/mongoutil"
|
||
|
"github.com/openimsdk/tools/errs"
|
||
12 months ago
|
"go.mongodb.org/mongo-driver/bson"
|
||
|
"go.mongodb.org/mongo-driver/mongo"
|
||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||
|
)
|
||
|
|
||
6 months ago
|
func NewS3Mongo(db *mongo.Database) (database.ObjectInfo, error) {
|
||
12 months ago
|
coll := db.Collection("s3")
|
||
|
_, err := coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{
|
||
|
Keys: bson.D{
|
||
|
{Key: "name", Value: 1},
|
||
|
},
|
||
|
Options: options.Index().SetUnique(true),
|
||
|
})
|
||
|
if err != nil {
|
||
9 months ago
|
return nil, errs.Wrap(err)
|
||
12 months ago
|
}
|
||
|
return &S3Mongo{coll: coll}, nil
|
||
|
}
|
||
|
|
||
|
type S3Mongo struct {
|
||
|
coll *mongo.Collection
|
||
|
}
|
||
|
|
||
6 months ago
|
func (o *S3Mongo) SetObject(ctx context.Context, obj *model.Object) error {
|
||
12 months ago
|
filter := bson.M{"name": obj.Name, "engine": obj.Engine}
|
||
|
update := bson.M{
|
||
|
"name": obj.Name,
|
||
|
"engine": obj.Engine,
|
||
|
"key": obj.Key,
|
||
|
"size": obj.Size,
|
||
|
"content_type": obj.ContentType,
|
||
|
"group": obj.Group,
|
||
|
"create_time": obj.CreateTime,
|
||
|
}
|
||
7 months ago
|
return mongoutil.UpdateOne(ctx, o.coll, filter, bson.M{"$set": update}, false, options.Update().SetUpsert(true))
|
||
12 months ago
|
}
|
||
|
|
||
6 months ago
|
func (o *S3Mongo) Take(ctx context.Context, engine string, name string) (*model.Object, error) {
|
||
12 months ago
|
if engine == "" {
|
||
6 months ago
|
return mongoutil.FindOne[*model.Object](ctx, o.coll, bson.M{"name": name})
|
||
12 months ago
|
}
|
||
6 months ago
|
return mongoutil.FindOne[*model.Object](ctx, o.coll, bson.M{"name": name, "engine": engine})
|
||
12 months ago
|
}
|
||
|
|
||
|
func (o *S3Mongo) Delete(ctx context.Context, engine string, name string) error {
|
||
7 months ago
|
return mongoutil.DeleteOne(ctx, o.coll, bson.M{"name": name, "engine": engine})
|
||
12 months ago
|
}
|