/
pgpfile.go
132 lines (112 loc) · 4.38 KB
/
pgpfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package model
import (
"context"
"errors"
"time"
"github.com/Arapgp/Arapgp-Server-go/config"
"github.com/Arapgp/Arapgp-Server-go/tool"
log "github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
)
const (
pgpFileConnName = "mongo"
pgpFileCollectionName = "pgpfile"
)
// PGPFile is file document type
type PGPFile struct {
// Name is used to figure out src & dst.
// namely, "repeat name" is forbidden
Name string `bson:"name"`
Author string `bson:"author"`
Size int `bson:"size"`
// Create_time represents when file sent to server
CreateTime time.Time `bson:"createtime"`
LastModifyTime time.Time `bson:"lastmodifytime"`
// Path means the place where this "File" stored
// and this path is a relative path.
// absolute path = path prefix + path (relative path)
Path string `bson:"path"`
PubKey string `bson:"pubkey"`
}
// InsertPGPFiles is to insert multi-files.
// input parameter(files) need init all properties
func InsertPGPFiles(files []PGPFile) (err error) {
// get collection
databaseName := config.DBcfg[pgpFileConnName].Database
pgpFileCollection := tool.GetClient(pgpFileConnName).Database(databaseName).Collection(pgpFileCollectionName)
// generate context
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
// simple convertion from []PGPFile to []interface{}
document := make([]interface{}, len(files))
for i, file := range files {
document[i] = file
}
// insert documents to pgpfile collection (converted from input)
// need insert documents to user.files in route.api
res, err := pgpFileCollection.InsertMany(ctx, document)
if err != nil {
errmsg := "arapgp.model.pgpfile => InsertPGPFiles: collection InsertMany failed;"
log.WithFields(log.Fields{"res": res, "err": err.Error()}).Warningln(errmsg)
return errors.New(errmsg + err.Error())
}
return nil
}
// GetPGPFiles will get Many PGPfiles from mongo.Collection
func GetPGPFiles(files []PGPFile, filter interface{}) (err error) {
// get collection
databaseName := config.DBcfg[userConnName].Database
pgpFileCollection := tool.GetClient(userConnName).Database(databaseName).Collection(pgpFileCollectionName)
// generate context
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
// generate cursor
cur, err := pgpFileCollection.Find(ctx, filter)
if err != nil {
errmsg := "arapgp.model.pgpfile => GetPGPFiles: collection Find failed"
log.WithFields(log.Fields{"cur": cur, "err": err.Error()}).Warningln(errmsg)
return errors.New(errmsg + err.Error())
}
defer cur.Close(ctx)
// cursor get all
if err = cur.All(ctx, &files); err != nil {
errmsg := "arapgp.model.pgpfile => GetPGPFile: cursor.All failed"
log.WithFields(log.Fields{"cur": cur, "err": err.Error()}).Warningln(errmsg)
return errors.New(errmsg + err.Error())
}
return
}
// UpdatePGPFiles will update all PGPFiles that get through filter
func UpdatePGPFiles(update bson.M, filter bson.M) (err error) {
// get collection
databaseName := config.DBcfg[userConnName].Database
pgpFileCollection := tool.GetClient(userConnName).Database(databaseName).Collection(pgpFileCollectionName)
// generate context
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
// update documents
res, err := pgpFileCollection.UpdateMany(ctx, filter, update)
if err != nil {
errmsg := "arapgp.model.pgpfile => UpdatePGPFiles: collection UpdateMany failed;"
log.WithFields(log.Fields{"res": res, "err": err.Error()}).Warningln(errmsg)
return errors.New(errmsg + err.Error())
}
return
}
// DeletePGPFiles will delete all PGPFiles that get through filter
func DeletePGPFiles(filter bson.M) (err error) {
// get collection
databaseName := config.DBcfg[userConnName].Database
pgpFileCollection := tool.GetClient(userConnName).Database(databaseName).Collection(pgpFileCollectionName)
// generate context
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
// delete documents
res, err := pgpFileCollection.DeleteMany(ctx, filter)
if err != nil {
errmsg := "arapgp.model.pgpfile => DeletePGPFiles: collection DeleteMany failed;"
log.WithFields(log.Fields{"res": res, "err": err.Error()}).Warningln(errmsg)
return errors.New(errmsg + err.Error())
}
return
}