forked from cfadmin-cn/mongo
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgridfs.lua
More file actions
Latest commit
168 lines (153 loc) · 5.82 KB
/
Copy pathgridfs.lua
File metadata and controls
168 lines (153 loc) · 5.82 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
localbson=require"mongo.bson"
localcrypt=require"crypt"
localmd5=crypt.md5
localhexencode=crypt.hexencode
localrandomkey=crypt.randomkey_ex
localprotocol=require"mongo.protocol"
localrequest_gridfs_files_list=protocol.request_gridfs_files_list
localrequest_gridfs_chunks_list=protocol.request_gridfs_chunks_list
localrequest_gridfs_files_delete=protocol.request_gridfs_files_delete
localrequest_gridfs_chunks_delete=protocol.request_gridfs_chunks_delete
localrequest_gridfs_files_upload=protocol.request_gridfs_files_upload
localrequest_gridfs_chunks_upload=protocol.request_gridfs_chunks_upload
localtype=type
localipairs=ipairs
localassert=assert
localio_type=io.type
localfmt=string.format
localtinsert=table.insert
localsys=require"sys"
localnew_tab=sys.new_tab
localclass=require"class"
localGRIDFS=class("GRIDFS")
functionGRIDFS:ctor(opt)
self.ctx=opt.ctx
end
---comment 上传
---@paramdatabasestring @需要上传的数据库名称
---@paramcollectstring @需要上传的集合名称
---@paramfilenamestring @文件名称
---@paramfilestring | file* @文件内容或者文件对象(文件对象需要自行关闭)
---@parammetatable @文件元数据信息, 一般情况下课忽略
functionGRIDFS:gridfs_upload(database, collect, filename, file, meta)
-- 检查文件名
iftype(filename) ~='string' orfilename=='' then
filename=randomkey(16, true)
end
-- 检查文件对象
ifio_type(file) =="file" then
file=file:read"*a"
end
localfilelen=#file
assert(type(file) =='string', "Invalid file content.")
-- 生成OID
localoid=hexencode(bson.objectid()())
localtab1, tab2, err
-- 开始写入数据
iffilelen<16777216then
tab2, err=request_gridfs_chunks_upload(self.ctx, database, collect, oid, file)
ifnottab2then
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab2.code, tab2.errmsg)
end
else
-- 大文件的分片传输实现.
locals, e=1, 16777216
locallist=new_tab(128, 0)
whiletruedo
list[#list+1] =file:sub(s, e)
ife>=filelenthen
break
end
s=e+1
e=e+16777216
end
localn=0
for_, contentinipairs(list) do
tab2, err=request_gridfs_chunks_upload(self.ctx, database, collect, oid, content)
ifnottab2then
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab2.code, tab2.errmsg)
end
n=n+tab2['n']
end
tab2["n"] =n
end
-- 数据插入完毕后再插入文件信息.
tab1, err=request_gridfs_files_upload(self.ctx, database, collect, oid, filename, filelen, md5(file, true), meta)
ifnottab1then
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab1.code, tab1.errmsg)
end
return { acknowledged=true, _id=oid, insertedCount=tab1["n"], sharedCount=tab2["n"] }
end
---comment 下载
---@paramdatabasestring @需要下载的数据库名称
---@paramcollectstring @需要下载的集合名称
---@paramfiltertable @过滤条件
functionGRIDFS:gridfs_download(database, collect, filter)
locallist=new_tab(128, 0)
localid=0
whiletruedo
localtab, err=request_gridfs_chunks_list(self.ctx, database, collect, type(filter) =='table' andfilteror {}, id)
ifnottabthen
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab.code, tab.errmsg)
end
localcursor=tab.cursor
for_, iteminipairs(cursor.firstBatchorcursor.nextBatch) do
-- item.data = nil
tinsert(list, item)
end
id=cursor.id
ifid==0then
break
end
end
returnlist
end
---comment 删除
---@paramdatabasestring @需要下载的数据库名称
---@paramcollectstring @需要下载的集合名称
---@paramfiltertable @过滤条件
functionGRIDFS:gridfs_delete(database, collect, filter, limit)
localtab1, tab2, err
tab1, err=request_gridfs_files_delete(self.ctx, database, collect, filter, limit)
ifnottab1then
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab1.code, tab1.errmsg)
end
-- var_dump(tab)
tab2, err=request_gridfs_chunks_delete(self.ctx, database, collect, filter, limit)
ifnottab2then
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab2.code, tab2.errmsg)
end
-- var_dump(tab)
-- return true
return { acknowledged=true, deletedCount=tab1.n==tab2.nandtab1.nortab2.n }
end
---comment 查询所有
---@paramdatabasestring @需要查询的数据库名称
---@paramcollectstring @需要查询的集合名称
---@paramidinteger @游标ID
functionGRIDFS:gridfs_findall(database, collect, id)
assert(type(database) =='string' anddatabase~='' andtype(collect) =='string' andcollect~='', "Invalid gridfs collect or database.")
localtab, err=request_gridfs_files_list(self.ctx, database, collect, {}, id)
ifnottabthen
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab.code, tab.errmsg)
end
returntab.cursor.firstBatchortab.cursor.nextBatch, tab.cursor.id
end
---comment 查询指定
---@paramdatabasestring @需要查询的数据库名称
---@paramcollectstring @需要查询的集合名称
---@paramfiltertable @过滤条件
---@paramidinteger @游标ID
functionGRIDFS:gridfs_find(database, collect, filter, id)
assert(type(database) =='string' anddatabase~='' andtype(collect) =='string' andcollect~='', "Invalid gridfs collect or database.")
localtab, err=request_gridfs_files_list(self.ctx, database, collect, filter, id)
ifnottabthen
returnfalse, errorfmt('{"errcode":%d,"errmsg":"%s"}', tab.code, tab.errmsg)
end
returntab.cursor.firstBatchortab.cursor.nextBatch, tab.cursor.id
end
-- 释放资源
functionGRIDFS:close()
self.ctx=nil
end
returnGRIDFS