@digione/node-custom-api 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/constants/header.d.ts +10 -0
- package/constants/header.js +2 -0
- package/constants/http.d.ts +44 -0
- package/constants/http.js +2 -0
- package/database/index.d.ts +3 -0
- package/database/index.js +34 -0
- package/database/redis.d.ts +15 -0
- package/database/redis.js +68 -0
- package/errors/authError.d.ts +55 -0
- package/errors/authError.js +57 -0
- package/errors/index.d.ts +32 -0
- package/errors/index.js +113 -0
- package/index.d.ts +1 -0
- package/index.js +4 -0
- package/interface/param.d.ts +41 -0
- package/interface/param.js +2 -0
- package/middlewares/accessToken.d.ts +13 -0
- package/middlewares/accessToken.js +200 -0
- package/middlewares/coreSite.d.ts +4 -0
- package/middlewares/coreSite.js +52 -0
- package/middlewares/paramQuery.d.ts +16 -0
- package/middlewares/paramQuery.js +220 -0
- package/middlewares/refSite.d.ts +2 -0
- package/middlewares/refSite.js +58 -0
- package/middlewares/refreshToken.d.ts +2 -0
- package/middlewares/refreshToken.js +56 -0
- package/middlewares/systemSite.d.ts +6 -0
- package/middlewares/systemSite.js +106 -0
- package/middlewares/webSite.d.ts +7 -0
- package/middlewares/webSite.js +109 -0
- package/models/core/city.d.ts +66 -0
- package/models/core/city.js +79 -0
- package/models/core/country.d.ts +35 -0
- package/models/core/country.js +44 -0
- package/models/core/district.d.ts +17 -0
- package/models/core/district.js +23 -0
- package/models/core/domain.d.ts +57 -0
- package/models/core/domain.js +75 -0
- package/models/core/site.d.ts +60 -0
- package/models/core/site.js +70 -0
- package/models/core/state.d.ts +49 -0
- package/models/core/state.js +58 -0
- package/models/core/zipcode.d.ts +5 -0
- package/models/core/zipcode.js +28 -0
- package/models/stream/field.d.ts +60 -0
- package/models/stream/field.js +66 -0
- package/models/stream/field_section.d.ts +33 -0
- package/models/stream/field_section.js +39 -0
- package/models/stream/stream.d.ts +89 -0
- package/models/stream/stream.js +97 -0
- package/models/system/currency.d.ts +5 -0
- package/models/system/currency.js +32 -0
- package/models/system/email_log.d.ts +5 -0
- package/models/system/email_log.js +54 -0
- package/models/system/email_sender.d.ts +22 -0
- package/models/system/email_sender.js +53 -0
- package/models/system/email_template.d.ts +5 -0
- package/models/system/email_template.js +49 -0
- package/models/system/file.d.ts +98 -0
- package/models/system/file.js +111 -0
- package/models/system/folder.d.ts +59 -0
- package/models/system/folder.js +78 -0
- package/models/system/language.d.ts +44 -0
- package/models/system/language.js +50 -0
- package/models/system/metadata.d.ts +5 -0
- package/models/system/metadata.js +24 -0
- package/models/system/module.d.ts +5 -0
- package/models/system/module.js +35 -0
- package/models/system/notification_log.d.ts +25 -0
- package/models/system/notification_log.js +76 -0
- package/models/system/notification_module.d.ts +5 -0
- package/models/system/notification_module.js +35 -0
- package/models/system/notification_setting.d.ts +22 -0
- package/models/system/notification_setting.js +44 -0
- package/models/system/notification_template.d.ts +5 -0
- package/models/system/notification_template.js +49 -0
- package/models/system/notification_type.d.ts +5 -0
- package/models/system/notification_type.js +38 -0
- package/models/system/payment_transaction.d.ts +5 -0
- package/models/system/payment_transaction.js +46 -0
- package/models/system/pdf_page.d.ts +5 -0
- package/models/system/pdf_page.js +39 -0
- package/models/system/pdf_template.d.ts +22 -0
- package/models/system/pdf_template.js +67 -0
- package/models/system/review_template.d.ts +5 -0
- package/models/system/review_template.js +35 -0
- package/models/system/review_vote.d.ts +5 -0
- package/models/system/review_vote.js +36 -0
- package/models/system/session.d.ts +5 -0
- package/models/system/session.js +19 -0
- package/models/system/setting.d.ts +15 -0
- package/models/system/setting.js +22 -0
- package/models/system/site_token.d.ts +25 -0
- package/models/system/site_token.js +31 -0
- package/models/system/sms_log.d.ts +4 -0
- package/models/system/sms_log.js +52 -0
- package/models/system/sms_setting.d.ts +4 -0
- package/models/system/sms_setting.js +39 -0
- package/models/system/sms_template.d.ts +5 -0
- package/models/system/sms_template.js +48 -0
- package/models/system/tax.d.ts +5 -0
- package/models/system/tax.js +33 -0
- package/models/system/website.d.ts +56 -0
- package/models/system/website.js +66 -0
- package/models/system/zone.d.ts +57 -0
- package/models/system/zone.js +141 -0
- package/models/user/access_token.d.ts +37 -0
- package/models/user/access_token.js +46 -0
- package/models/user/address.d.ts +69 -0
- package/models/user/address.js +119 -0
- package/models/user/b2b_group.d.ts +57 -0
- package/models/user/b2b_group.js +63 -0
- package/models/user/group.d.ts +28 -0
- package/models/user/group.js +34 -0
- package/models/user/group_permission.d.ts +4 -0
- package/models/user/group_permission.js +27 -0
- package/models/user/notification_token.d.ts +32 -0
- package/models/user/notification_token.js +39 -0
- package/models/user/organ.d.ts +85 -0
- package/models/user/organ.js +100 -0
- package/models/user/profile.d.ts +31 -0
- package/models/user/profile.js +37 -0
- package/models/user/refresh_token.d.ts +41 -0
- package/models/user/refresh_token.js +51 -0
- package/models/user/team.d.ts +59 -0
- package/models/user/team.js +71 -0
- package/models/user/user.d.ts +79 -0
- package/models/user/user.js +100 -0
- package/models/user/user_organ.d.ts +52 -0
- package/models/user/user_organ.js +87 -0
- package/package.json +59 -0
- package/utils/auth.d.ts +25 -0
- package/utils/auth.js +137 -0
- package/utils/config.d.ts +8 -0
- package/utils/config.js +6 -0
- package/utils/core.d.ts +6 -0
- package/utils/core.js +39 -0
- package/utils/currency.d.ts +16 -0
- package/utils/currency.js +38 -0
- package/utils/db.d.ts +94 -0
- package/utils/db.js +399 -0
- package/utils/file.d.ts +232 -0
- package/utils/file.js +772 -0
- package/utils/helper.d.ts +113 -0
- package/utils/helper.js +508 -0
- package/utils/index.d.ts +10 -0
- package/utils/index.js +13 -0
- package/utils/language.d.ts +18 -0
- package/utils/language.js +48 -0
- package/utils/metadata.d.ts +18 -0
- package/utils/metadata.js +50 -0
- package/utils/session.d.ts +37 -0
- package/utils/session.js +60 -0
- package/utils/setting.d.ts +14 -0
- package/utils/setting.js +70 -0
- package/utils/stream.d.ts +303 -0
- package/utils/stream.js +1893 -0
- package/utils/user.d.ts +61 -0
- package/utils/user.js +282 -0
- package/utils/validator.d.ts +56 -0
- package/utils/validator.js +269 -0
package/utils/file.js
ADDED
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FileUtil = exports.getBuffer = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const file_1 = require("../models/system/file");
|
|
6
|
+
const folder_1 = require("../models/system/folder");
|
|
7
|
+
const errors_1 = require("../errors");
|
|
8
|
+
const config_1 = require("./config");
|
|
9
|
+
const helper_1 = require("./helper");
|
|
10
|
+
const db_1 = require("./db");
|
|
11
|
+
const setting_1 = require("./setting");
|
|
12
|
+
const sequelize_1 = require("sequelize");
|
|
13
|
+
const aws = require("aws-sdk");
|
|
14
|
+
const jimp = require("jimp/dist");
|
|
15
|
+
const mime = require("mime-types");
|
|
16
|
+
const _ = require("lodash");
|
|
17
|
+
const uuid_1 = require("uuid");
|
|
18
|
+
const path = require("path");
|
|
19
|
+
const fs = require("fs");
|
|
20
|
+
const getBuffer = (image, { mimetype = "" } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
+
if (mimetype === "gif") {
|
|
22
|
+
mimetype = jimp.MIME_PNG;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
mimetype = jimp.AUTO;
|
|
26
|
+
}
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
image.getBuffer(mimetype, (err, buffer) => {
|
|
29
|
+
if (err) {
|
|
30
|
+
reject(err);
|
|
31
|
+
}
|
|
32
|
+
resolve(buffer);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
exports.getBuffer = getBuffer;
|
|
37
|
+
class FileUtil {
|
|
38
|
+
constructor(ref = "", option) {
|
|
39
|
+
this.location = (0, helper_1.getENV)('AWS_LOCATION', 'default');
|
|
40
|
+
this.bucket = (0, helper_1.getENV)('AWS_BUCKET');
|
|
41
|
+
this.ACL = (0, helper_1.getENV)('AWS_ACL', 'public-read');
|
|
42
|
+
this.ref = ref;
|
|
43
|
+
this.option = option || {};
|
|
44
|
+
this.settingUtil = new setting_1.SettingUtil(this.ref);
|
|
45
|
+
}
|
|
46
|
+
getStorage() {
|
|
47
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
let storage = { driver: this.driver, setting: this.setting };
|
|
49
|
+
if (!storage['setting']) {
|
|
50
|
+
this.setting = yield this.settingUtil.findSetting(["files_driver", "files_url", "files_object_access_key", "files_object_secret_key", "files_object_url"]);
|
|
51
|
+
this.driver = this.setting["files_driver"];
|
|
52
|
+
this.setting = _.omit(this.setting, ["files_driver"]);
|
|
53
|
+
storage['setting'] = this.setting;
|
|
54
|
+
if (this.driver == "object") {
|
|
55
|
+
this.driver = "local";
|
|
56
|
+
if (this.setting['files_object_secret_key'] && this.setting['files_object_access_key'] && this.setting['files_object_url']) {
|
|
57
|
+
this.driver = "object";
|
|
58
|
+
let url = this.setting['files_object_url'].replace(/ /g, "");
|
|
59
|
+
this.url = String(url).replace("{{bucket}}", this.bucket);
|
|
60
|
+
this.setting['files_object_url'] = url.replace("https://{{bucket}}.", "");
|
|
61
|
+
this.s3 = new aws.S3({
|
|
62
|
+
secretAccessKey: this.setting['files_object_secret_key'],
|
|
63
|
+
accessKeyId: this.setting['files_object_access_key'],
|
|
64
|
+
endpoint: this.setting['files_object_url']
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
storage['driver'] = this.driver;
|
|
69
|
+
}
|
|
70
|
+
return storage;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
getFileAttribute() {
|
|
74
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
return yield (0, db_1.getFieldAll)(this.ref + "_" + file_1.FileModel.getTableName());
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
getFilePath(item) {
|
|
79
|
+
let result = [item['path']], thumb = item['thumb'] || {};
|
|
80
|
+
result = result.concat(Object.keys(thumb).reduce((total, key) => total.concat(thumb[key]), []));
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
getSettingImgKit() {
|
|
84
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
85
|
+
let setting = yield this.settingUtil.findSetting(['files_img_kit_url', 'files_img_kit', 'files_img_kit_default_param', 'files_object_url']);
|
|
86
|
+
if (setting && (setting['files_img_kit'] == '1' && setting['files_img_kit_url'])) {
|
|
87
|
+
let origin = "";
|
|
88
|
+
if (setting['files_object_url']) {
|
|
89
|
+
origin = setting['files_object_url'].replace(/ /g, "").replace("{{bucket}}", this.bucket);
|
|
90
|
+
}
|
|
91
|
+
setting = { url: setting['files_img_kit_url'], origin, default_param: setting['files_img_kit_default_param'] };
|
|
92
|
+
}
|
|
93
|
+
return setting;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
convertImgKit(path, setting = {}) {
|
|
97
|
+
if (typeof path != "string")
|
|
98
|
+
return path;
|
|
99
|
+
let rep = setting['origin'] || this.url;
|
|
100
|
+
let temp = path.replace(rep, setting['url']);
|
|
101
|
+
if (temp != path) {
|
|
102
|
+
path = temp;
|
|
103
|
+
if (setting['default_param']) {
|
|
104
|
+
path = path + setting['default_param'];
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return path;
|
|
108
|
+
}
|
|
109
|
+
afterQueryFile(data = {}, { tag = {}, file_link = false } = {}) {
|
|
110
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
data['custom'] = data['custom'] || {};
|
|
112
|
+
data['tags'] = data['tags'] || [];
|
|
113
|
+
if (data['filename'] == data['path']) {
|
|
114
|
+
data['path'] = (this.setting['files_url'] || this.option.hostname) + '/uploads/' + data['path'];
|
|
115
|
+
}
|
|
116
|
+
if (Object.keys(tag).length && (data['tags'] && data['tags'].length)) {
|
|
117
|
+
data['tags'] = data['tags'].map(i => {
|
|
118
|
+
if (tag[i['slug']])
|
|
119
|
+
i['title'] = tag[i['slug']];
|
|
120
|
+
return i;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
if (file_link && data['file_link'] == 'yes' && data['file_id']) {
|
|
124
|
+
data['file_data'] = yield file_1.FileModel.schema(this.ref, "_").findOne({
|
|
125
|
+
where: { id: data['file_id'] }, raw: true
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return data;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
uploadRaw(files) {
|
|
132
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
133
|
+
let reqs = [], results = [];
|
|
134
|
+
for (let file of files) {
|
|
135
|
+
let extension = mime.extension(file.mimetype);
|
|
136
|
+
let input = {
|
|
137
|
+
Bucket: this.bucket,
|
|
138
|
+
Key: this.ref + "/" + (0, helper_1.getDateTime)().format('X') + '.' + (0, helper_1.getSlug)(32) + '.' + extension,
|
|
139
|
+
Body: file.buffer,
|
|
140
|
+
ACL: this.ACL,
|
|
141
|
+
ContentType: file.mimetype
|
|
142
|
+
};
|
|
143
|
+
let type = yield this.allowedFileType(extension);
|
|
144
|
+
reqs.push(Object.assign(Object.assign(Object.assign({}, input), file), { type, extension: "." + extension }));
|
|
145
|
+
results.push(input);
|
|
146
|
+
}
|
|
147
|
+
results = yield this.upload(results);
|
|
148
|
+
return _.merge(reqs, results);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
upload(files) {
|
|
152
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
153
|
+
if (files.length) {
|
|
154
|
+
try {
|
|
155
|
+
let storage = yield this.getStorage();
|
|
156
|
+
let data = yield Promise.all(files.map((file) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
157
|
+
if (storage['driver'] == "object") {
|
|
158
|
+
let res = yield this.s3.upload(file).promise();
|
|
159
|
+
return Object.assign(Object.assign({}, res), file);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
let destination = path.join(config_1.config.fileDestination, file.Key);
|
|
163
|
+
let segment = destination.split('/'), dir = segment.slice(0, segment.length - 1).join('/');
|
|
164
|
+
let exist = yield fs.existsSync(dir);
|
|
165
|
+
if (!exist) {
|
|
166
|
+
yield fs.mkdirSync(dir, { recursive: true });
|
|
167
|
+
}
|
|
168
|
+
yield fs.writeFileSync(destination, file.Body);
|
|
169
|
+
return Object.assign({ Location: file.Key }, file);
|
|
170
|
+
}
|
|
171
|
+
})));
|
|
172
|
+
return data;
|
|
173
|
+
}
|
|
174
|
+
catch (err) {
|
|
175
|
+
return Promise.reject(errors_1.generalError.BAD_REQUEST);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return Promise.reject(errors_1.generalError.BAD_REQUEST);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
uploadThumb(item, title, { extension = "", width = 0, height = 0 } = {}) {
|
|
182
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
183
|
+
let files = [];
|
|
184
|
+
let image, buffer;
|
|
185
|
+
try {
|
|
186
|
+
image = yield jimp.read(item);
|
|
187
|
+
image.resize(width * 2, jimp.AUTO);
|
|
188
|
+
buffer = yield (0, exports.getBuffer)(image, { mimetype: extension.split('.')[1] });
|
|
189
|
+
}
|
|
190
|
+
catch (err) {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
files.push({
|
|
194
|
+
Bucket: this.bucket,
|
|
195
|
+
Key: title,
|
|
196
|
+
Body: buffer,
|
|
197
|
+
ACL: this.ACL,
|
|
198
|
+
ContentType: "image/" + extension.split('.')[1]
|
|
199
|
+
});
|
|
200
|
+
let results = yield this.upload(files);
|
|
201
|
+
return results[0].Location;
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
existImageThumb(item, { width = 0, height = 0, checkExist = true } = {}) {
|
|
205
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
206
|
+
if (item && item['filename']) {
|
|
207
|
+
let segment = item['filename'].split("/");
|
|
208
|
+
let filename = segment[segment.length - 1];
|
|
209
|
+
segment = filename.split(".");
|
|
210
|
+
filename = filename.replace("." + segment[segment.length - 1], "");
|
|
211
|
+
let img_thumb = this.ref + '/thumb/' + filename + '_' + width + 'x' + height + item['extension'];
|
|
212
|
+
let storage = yield this.getStorage();
|
|
213
|
+
if (storage['driver'] == "object") {
|
|
214
|
+
if (checkExist) {
|
|
215
|
+
try {
|
|
216
|
+
yield this.s3.headObject({
|
|
217
|
+
Bucket: this.bucket,
|
|
218
|
+
Key: img_thumb
|
|
219
|
+
}).promise();
|
|
220
|
+
return this.url + img_thumb;
|
|
221
|
+
}
|
|
222
|
+
catch (err) { }
|
|
223
|
+
}
|
|
224
|
+
return yield this.uploadThumb(item['path'], img_thumb, { extension: item['extension'], width, height });
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
if (checkExist) {
|
|
228
|
+
let destination = path.join(config_1.config.fileDestination, img_thumb);
|
|
229
|
+
let exist = yield fs.existsSync(destination);
|
|
230
|
+
if (exist) {
|
|
231
|
+
return img_thumb;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
try {
|
|
235
|
+
let destination = path.join(config_1.config.fileDestination, item['filename']);
|
|
236
|
+
let buffer = yield fs.readFileSync(destination);
|
|
237
|
+
return yield this.uploadThumb(buffer, img_thumb, { extension: item['extension'], width, height });
|
|
238
|
+
}
|
|
239
|
+
catch (err) { }
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return null;
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
findImageThumb(item, { width = 0, height = 0, upsert = true }) {
|
|
246
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
247
|
+
if (item) {
|
|
248
|
+
let result = item['thumb'] || {}, path = item['path'];
|
|
249
|
+
if (typeof item['thumb'] == "undefined") {
|
|
250
|
+
let query = yield file_1.FileModel.schema(this.ref, "_").findOne({ where: { id: item['id'] }, attributes: ['path', 'thumb'], raw: true });
|
|
251
|
+
if (query)
|
|
252
|
+
path = query['path'], result = query['thumb'] || {};
|
|
253
|
+
}
|
|
254
|
+
let thumb = result[width + 'x' + height] || (yield this.existImageThumb(item, { width, height }));
|
|
255
|
+
if (upsert && !result[width + 'x' + height] && item['id']) {
|
|
256
|
+
result[width + 'x' + height] = thumb;
|
|
257
|
+
yield (0, db_1.update)(this.ref, file_1.FileModel.getTableName().toString(), { thumb: JSON.stringify(result) }, { where: { id: item['id'] } });
|
|
258
|
+
}
|
|
259
|
+
if (item['filename'] == path) {
|
|
260
|
+
yield this.getStorage();
|
|
261
|
+
let data = yield this.afterQueryFile({ filename: thumb, path: thumb });
|
|
262
|
+
thumb = data.path;
|
|
263
|
+
}
|
|
264
|
+
return thumb;
|
|
265
|
+
}
|
|
266
|
+
return null;
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
findImageThumbByFolderSlug(slug, { where = {}, width = 0, height = 0, parent_folder = "", tags = [], group = null } = {}) {
|
|
270
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
271
|
+
try {
|
|
272
|
+
if (parent_folder) {
|
|
273
|
+
let query = yield folder_1.FolderModel.schema(this.ref, "_").findOne({ where: { slug: parent_folder }, attributes: ['id'], raw: true });
|
|
274
|
+
if (!query)
|
|
275
|
+
return null;
|
|
276
|
+
where['$folder.parent_id$'] = query['id'];
|
|
277
|
+
}
|
|
278
|
+
where = this.beforeFindFile(where, { tags, group });
|
|
279
|
+
let result = yield file_1.FileModel.schema(this.ref, "_").findOne({
|
|
280
|
+
raw: true, attributes: ['id', 'extension', 'filename', 'path', 'thumb'],
|
|
281
|
+
where: Object.assign({ '$folder.slug$': slug }, where),
|
|
282
|
+
include: [{ as: 'folder', model: folder_1.FolderModel.schema(this.ref, "_"), attributes: [] }],
|
|
283
|
+
order: [['sort', 'asc'], ['tags', 'desc']]
|
|
284
|
+
});
|
|
285
|
+
return this.findImageThumb(result, { width, height });
|
|
286
|
+
}
|
|
287
|
+
catch (err) {
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
findImageThumbByFileId(id, { width = 0, height = 0 }) {
|
|
293
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
294
|
+
const data = yield file_1.FileModel.schema(this.ref, "_").findOne({
|
|
295
|
+
where: { id }, attributes: ['id', 'extension', 'filename', 'path', 'thumb']
|
|
296
|
+
});
|
|
297
|
+
let item = data.toJSON();
|
|
298
|
+
return this.findImageThumb(item, { width, height });
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
beforeFindFile(where = {}, { tags = [], group = undefined } = {}) {
|
|
302
|
+
if (this.option.organ_id) {
|
|
303
|
+
where[sequelize_1.Op.and] = (where[sequelize_1.Op.and] || []).concat({ [sequelize_1.Op.or]: [{ organ_id: this.option.organ_id }, { organ_by: this.option.organ_id }] });
|
|
304
|
+
}
|
|
305
|
+
if (typeof group != "undefined") {
|
|
306
|
+
where['group'] = group;
|
|
307
|
+
}
|
|
308
|
+
if (tags.length) {
|
|
309
|
+
let op = tags.reduce((total, tag) => {
|
|
310
|
+
let value = Object.keys(tag).reduce((total, key) => total.concat([`'${key}'`, `"${tag[key]}"`]), []).join(',');
|
|
311
|
+
if (value) {
|
|
312
|
+
total = total.concat((0, sequelize_1.literal)(`JSON_CONTAINS(tags,JSON_OBJECT(${value}))`));
|
|
313
|
+
if (tag['slug'] == "thumbnail") {
|
|
314
|
+
total = total.concat((0, sequelize_1.literal)(`tags IS NULL`));
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return total;
|
|
318
|
+
}, []);
|
|
319
|
+
if (op.length) {
|
|
320
|
+
where[sequelize_1.Op.and] = (where[sequelize_1.Op.and] || []).concat({ [sequelize_1.Op.or]: op });
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return where;
|
|
324
|
+
}
|
|
325
|
+
findAllFile({ where = {}, folder = false, full = false, file_link = false, thumb = false, width = 0, height = 0, parent_folder = "", attributes = undefined, limit = undefined, offset = undefined, tag = {}, tags = [], group = undefined } = {}) {
|
|
326
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
327
|
+
try {
|
|
328
|
+
let include = [];
|
|
329
|
+
if (parent_folder || folder)
|
|
330
|
+
include.push({ as: 'folder', model: folder_1.FolderModel.schema(this.ref, "_"), attributes: [] });
|
|
331
|
+
if (parent_folder) {
|
|
332
|
+
let query = yield folder_1.FolderModel.schema(this.ref, "_").findOne({ where: { slug: parent_folder }, attributes: ['id'], raw: true });
|
|
333
|
+
if (!query)
|
|
334
|
+
return [];
|
|
335
|
+
where['$folder.parent_id$'] = query['id'];
|
|
336
|
+
}
|
|
337
|
+
where = this.beforeFindFile(where, { tags, group });
|
|
338
|
+
where['is_hidden'] = where['is_hidden'] || "no";
|
|
339
|
+
let result = yield file_1.FileModel.schema(this.ref, "_").findAll({
|
|
340
|
+
attributes, raw: true, where, limit, offset, include, order: [['sort', 'asc'], ['tags', 'desc']]
|
|
341
|
+
});
|
|
342
|
+
result.length && (yield this.getStorage());
|
|
343
|
+
result = yield Promise.all(result.map((item) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
344
|
+
let include = {};
|
|
345
|
+
if (thumb && item['type'] == 'i') {
|
|
346
|
+
include['thumb'] = yield this.findImageThumb(item, { width, height });
|
|
347
|
+
}
|
|
348
|
+
let data = yield this.afterQueryFile(Object.assign(Object.assign({}, item), include), { tag, file_link });
|
|
349
|
+
if (!full) {
|
|
350
|
+
data = data.path;
|
|
351
|
+
}
|
|
352
|
+
return data;
|
|
353
|
+
})));
|
|
354
|
+
return result;
|
|
355
|
+
}
|
|
356
|
+
catch (err) {
|
|
357
|
+
return [];
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
findAndCountAllFileByFolder(slug, { where = {}, file_link = false, parent_folder = "", limit = undefined, offset = undefined, tag = {}, tags = [], group = null } = {}) {
|
|
362
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
363
|
+
let where_folder = {};
|
|
364
|
+
if (parent_folder) {
|
|
365
|
+
let query = yield folder_1.FolderModel.schema(this.ref, "_").findOne({ where: { slug: parent_folder }, attributes: ['id'], raw: true });
|
|
366
|
+
if (!query)
|
|
367
|
+
return { rows: [], count: 0 };
|
|
368
|
+
where_folder['parent_id'] = query['id'];
|
|
369
|
+
}
|
|
370
|
+
let folder = yield folder_1.FolderModel.schema(this.ref, "_").findOne({ where: Object.assign({ slug }, where_folder), attributes: ['id'], raw: true });
|
|
371
|
+
if (!folder)
|
|
372
|
+
return { rows: [], count: 0 };
|
|
373
|
+
where['folder_id'] = folder['id'];
|
|
374
|
+
where = this.beforeFindFile(where, { tags, group });
|
|
375
|
+
where['is_hidden'] = where['is_hidden'] || "no";
|
|
376
|
+
let data = yield Promise.all([this.findAllFile({ where, full: true, file_link, limit, offset, tag, group }), file_1.FileModel.schema(this.ref, "_").count({ where })]);
|
|
377
|
+
return { rows: data[0], count: data[1] };
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
findAllFileByFolder(slug, { where = {}, full = false, file_link = false, thumb = false, width = 0, height = 0, parent_folder = "", limit = undefined, offset = undefined, tags = [], group = null } = {}) {
|
|
381
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
382
|
+
return yield this.findAllFile({ where: Object.assign({ '$folder.slug$': slug }, where), folder: true, full, file_link, thumb, width, height, parent_folder, limit, offset, tags, group });
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
findOneFileByFolder(slug, { where = {}, full = false, file_link = false, parent_folder = "", tags = [], group = null } = {}) {
|
|
386
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
387
|
+
return (yield this.findAllFile({ where: Object.assign({ '$folder.slug$': slug }, where), folder: true, full, file_link, parent_folder, limit: 1, tags, group }))[0] || null;
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
findOneFileById(id, { where = {}, full = true, file_link = false, parent_folder = "", tags = [], group = null } = {}) {
|
|
391
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
392
|
+
return (yield this.findAllFile({ where: Object.assign({ id }, where), full, file_link, parent_folder, limit: 1, tags, group }))[0] || null;
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
uploadByReq(req, slug, user_id, { parent_id = 0, parent_slug = "", name = undefined, root = false, thumb = false, width = 0, height = 0, input = {}, is_hidden = undefined, tags = undefined, group = undefined } = {}) {
|
|
396
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
397
|
+
req.files = req.file ? [req.file] : (req.files || []);
|
|
398
|
+
if (req.files.length && slug) {
|
|
399
|
+
let files = [], reqs = [];
|
|
400
|
+
if (parent_slug) {
|
|
401
|
+
let query = yield this.createFolder(parent_slug, { root: true });
|
|
402
|
+
if (query) {
|
|
403
|
+
parent_id = query['id'];
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
let folder = yield this.createFolder(slug, { parent_id, root, name });
|
|
407
|
+
for (let file of req.files) {
|
|
408
|
+
let extension = mime.extension(file.mimetype);
|
|
409
|
+
let input = {
|
|
410
|
+
Bucket: this.bucket,
|
|
411
|
+
Key: this.ref + "/" + (0, helper_1.getDateTime)().format('X') + '.' + (0, helper_1.getSlug)(32) + '.' + extension,
|
|
412
|
+
Body: file.buffer,
|
|
413
|
+
ACL: this.ACL,
|
|
414
|
+
ContentType: file.mimetype
|
|
415
|
+
};
|
|
416
|
+
let type = yield this.allowedFileType(extension);
|
|
417
|
+
files.push(input);
|
|
418
|
+
reqs.push(Object.assign(Object.assign(Object.assign({}, file), input), { type }));
|
|
419
|
+
}
|
|
420
|
+
let results = yield this.upload(files);
|
|
421
|
+
results = yield this.formatFile(folder['id'], user_id, reqs, results, { input, is_hidden, tags, group });
|
|
422
|
+
return yield this.insertFileUnique(results, { thumb, width, height });
|
|
423
|
+
}
|
|
424
|
+
return Promise.reject(errors_1.generalError.BAD_REQUEST);
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
createFolder(slug, { parent_id = 0, root = false, name = undefined, location = this.location, remote_container = this.bucket, hidden = 0, input = {} } = {}) {
|
|
428
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
429
|
+
let where = {};
|
|
430
|
+
let storage = yield this.getStorage();
|
|
431
|
+
location = (storage['driver'] == 'object') ? location : 'local';
|
|
432
|
+
if (!root && this.option.organ_id) {
|
|
433
|
+
where[sequelize_1.Op.and] = (where[sequelize_1.Op.and] || []).concat({ [sequelize_1.Op.or]: [{ organ_id: this.option.organ_id }, { organ_by: this.option.organ_id }] });
|
|
434
|
+
}
|
|
435
|
+
let query = yield folder_1.FolderModel.schema(this.ref, "_").findOne({ where: Object.assign({ slug, parent_id, location }, where), attributes: ['id'], raw: true });
|
|
436
|
+
if (query) {
|
|
437
|
+
return query;
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
input = Object.assign(Object.assign({}, input), { slug,
|
|
441
|
+
parent_id, name: name || _.startCase(_.camelCase(slug)), location, remote_container: (storage['driver'] == 'object') ? remote_container : '', hidden });
|
|
442
|
+
if (!root) {
|
|
443
|
+
if (this.option.organ_id) {
|
|
444
|
+
input['organ_id'] = this.option.organ_id;
|
|
445
|
+
}
|
|
446
|
+
if (this.option.parent_organ_id) {
|
|
447
|
+
input['organ_by'] = this.option.parent_organ_id;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
let query = yield folder_1.FolderModel.schema(this.ref, "_").create(input);
|
|
451
|
+
if (!query) {
|
|
452
|
+
return Promise.reject(errors_1.generalError.BAD_REQUEST);
|
|
453
|
+
}
|
|
454
|
+
return query.toJSON();
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
createFileByPath(files, slug, user_id, { parent_slug = "", type = "i", name = undefined, root = false, upload = false, tags = undefined, group = undefined } = {}) {
|
|
459
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
460
|
+
let parent_id = null, input = {};
|
|
461
|
+
if ((files && files.length) && slug) {
|
|
462
|
+
if (parent_slug) {
|
|
463
|
+
let query = yield this.createFolder(parent_slug, { root: true });
|
|
464
|
+
if (query) {
|
|
465
|
+
parent_id = query['id'];
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if (upload)
|
|
469
|
+
input['upload'] = 1;
|
|
470
|
+
let folder = yield this.createFolder(slug, { parent_id, root, name });
|
|
471
|
+
let results = [], reqs = [];
|
|
472
|
+
files.forEach((item) => {
|
|
473
|
+
results.push({ Location: item, Key: item });
|
|
474
|
+
reqs.push({ originalname: (0, helper_1.getSlug)(32), mimetype: null, size: 0, type });
|
|
475
|
+
});
|
|
476
|
+
results = yield this.formatFile(folder['id'], user_id, reqs, results, { input, tags, group });
|
|
477
|
+
return yield this.insertFileUnique(results);
|
|
478
|
+
}
|
|
479
|
+
return Promise.reject(errors_1.generalError.BAD_REQUEST);
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
allowedFileType(extension) {
|
|
483
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
484
|
+
let type;
|
|
485
|
+
switch (extension) {
|
|
486
|
+
case "mpga":
|
|
487
|
+
case "mp2":
|
|
488
|
+
case "mp3":
|
|
489
|
+
case "ra":
|
|
490
|
+
case "rv":
|
|
491
|
+
case "wav":
|
|
492
|
+
type = 'a';
|
|
493
|
+
break;
|
|
494
|
+
case "mpeg":
|
|
495
|
+
case "mpg":
|
|
496
|
+
case "mpe":
|
|
497
|
+
case "mp4":
|
|
498
|
+
case "flv":
|
|
499
|
+
case "qt":
|
|
500
|
+
case "mov":
|
|
501
|
+
case "avi":
|
|
502
|
+
case "movie":
|
|
503
|
+
type = 'v';
|
|
504
|
+
break;
|
|
505
|
+
case "pdf":
|
|
506
|
+
case "xls":
|
|
507
|
+
case "ppt":
|
|
508
|
+
case "pptx":
|
|
509
|
+
case "txt":
|
|
510
|
+
case "text":
|
|
511
|
+
case "log":
|
|
512
|
+
case "rtx":
|
|
513
|
+
case "rtf":
|
|
514
|
+
case "xml":
|
|
515
|
+
case "json":
|
|
516
|
+
case "xsl":
|
|
517
|
+
case "doc":
|
|
518
|
+
case "docx":
|
|
519
|
+
case "xlsx":
|
|
520
|
+
case "word":
|
|
521
|
+
case "xl":
|
|
522
|
+
case "csv":
|
|
523
|
+
case "pages":
|
|
524
|
+
case "numbers":
|
|
525
|
+
type = 'd';
|
|
526
|
+
break;
|
|
527
|
+
case "bmp":
|
|
528
|
+
case "gif":
|
|
529
|
+
case "jpeg":
|
|
530
|
+
case "jpg":
|
|
531
|
+
case "jpe":
|
|
532
|
+
case "png":
|
|
533
|
+
case "tiff":
|
|
534
|
+
case "tif":
|
|
535
|
+
case "ico":
|
|
536
|
+
type = 'i';
|
|
537
|
+
break;
|
|
538
|
+
case "psd":
|
|
539
|
+
case "gtar":
|
|
540
|
+
case "swf":
|
|
541
|
+
case "tar":
|
|
542
|
+
case "tgz":
|
|
543
|
+
case "xhtml":
|
|
544
|
+
case "zip":
|
|
545
|
+
case "css":
|
|
546
|
+
case "html":
|
|
547
|
+
case "htm":
|
|
548
|
+
case "shtml":
|
|
549
|
+
case "svg":
|
|
550
|
+
case "rar":
|
|
551
|
+
type = 'o';
|
|
552
|
+
break;
|
|
553
|
+
}
|
|
554
|
+
if (!type) {
|
|
555
|
+
return Promise.reject(errors_1.generalError.FILE_TYPE_NOT_ALLOW);
|
|
556
|
+
}
|
|
557
|
+
return type;
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
formatFile(folder_id, user_id, reqs, files, { use_id = true, field = [], formatDateTime = "X", input = {}, is_hidden = undefined, tags = undefined, group = undefined } = {}) {
|
|
561
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
562
|
+
try {
|
|
563
|
+
let result = yield Promise.all(files.map((file, key) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
564
|
+
folder_id = reqs[key].folder ? reqs[key].folder : folder_id;
|
|
565
|
+
let id = (0, uuid_1.v4)().replace(/-/gi, '').substring(0, 15);
|
|
566
|
+
let organ_id = this.option.organ_id;
|
|
567
|
+
let organ_by = this.option.parent_organ_id;
|
|
568
|
+
let sort = (0, helper_1.getDateTime)().format(formatDateTime);
|
|
569
|
+
let date_added = sort;
|
|
570
|
+
let path = file.Location;
|
|
571
|
+
if (path && path != file.Key) {
|
|
572
|
+
let protocol = 'https://';
|
|
573
|
+
let index = path.indexOf(protocol);
|
|
574
|
+
path = (index < 0) ? protocol + path : path;
|
|
575
|
+
}
|
|
576
|
+
let extension = mime.extension(reqs[key].mimetype);
|
|
577
|
+
let format = Object.assign(Object.assign({}, input), { folder_id,
|
|
578
|
+
user_id,
|
|
579
|
+
organ_id,
|
|
580
|
+
organ_by,
|
|
581
|
+
sort,
|
|
582
|
+
date_added, name: reqs[key].originalname || (0, helper_1.getSlug)(32), filename: reqs[key].Key || file.Key || input['filename'], path, mimetype: reqs[key].mimetype, filesize: reqs[key].size, extension: (extension ? "." + extension : ''), type: reqs[key].type || file.type || input['type'], is_hidden,
|
|
583
|
+
tags,
|
|
584
|
+
group });
|
|
585
|
+
if (use_id) {
|
|
586
|
+
format['id'] = id;
|
|
587
|
+
}
|
|
588
|
+
if (reqs[key].mimetype && reqs[key].mimetype.split('/')[0] == "image") {
|
|
589
|
+
try {
|
|
590
|
+
let body = reqs[key].Body || reqs[key].buffer;
|
|
591
|
+
let image = yield jimp.read(body);
|
|
592
|
+
format['height'] = image.bitmap.height;
|
|
593
|
+
format['width'] = image.bitmap.width;
|
|
594
|
+
}
|
|
595
|
+
catch (err) { }
|
|
596
|
+
}
|
|
597
|
+
if (field.length) {
|
|
598
|
+
format = _.pick(format, field);
|
|
599
|
+
}
|
|
600
|
+
return format;
|
|
601
|
+
})));
|
|
602
|
+
return result;
|
|
603
|
+
}
|
|
604
|
+
catch (err) {
|
|
605
|
+
return null;
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
insertFileUnique(results, { thumb = false, width = 0, height = 0 } = {}) {
|
|
610
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
611
|
+
try {
|
|
612
|
+
let data = yield file_1.FileModel.schema(this.ref, "_").bulkCreate(results);
|
|
613
|
+
if (!data) {
|
|
614
|
+
return Promise.reject(errors_1.generalError.BAD_REQUEST);
|
|
615
|
+
}
|
|
616
|
+
let result = data.map(item => item.toJSON());
|
|
617
|
+
if (thumb) {
|
|
618
|
+
// create thumb
|
|
619
|
+
result = yield Promise.all(result.map((item) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
620
|
+
if (item['type'] == 'i') {
|
|
621
|
+
item['thumb'] = null;
|
|
622
|
+
let thumb = yield this.findImageThumb(item, { width, height });
|
|
623
|
+
return Object.assign(Object.assign({}, item), { thumb });
|
|
624
|
+
}
|
|
625
|
+
return item;
|
|
626
|
+
})));
|
|
627
|
+
}
|
|
628
|
+
return result;
|
|
629
|
+
}
|
|
630
|
+
catch (err) {
|
|
631
|
+
if (err.errors && err.errors[0].validatorKey == "not_unique") {
|
|
632
|
+
results = results.map(item => (Object.assign(Object.assign({}, item), { id: (0, uuid_1.v4)().replace(/-/gi, '').substring(0, 15) })));
|
|
633
|
+
return yield this.insertFileUnique(results, { thumb, width, height });
|
|
634
|
+
}
|
|
635
|
+
return Promise.reject(errors_1.generalError.BAD_REQUEST);
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
uploadRawFormat(files, { field = ['name', 'filename', 'path', 'mimetype', 'filesize', 'extension', 'type', 'height', 'width'] } = {}) {
|
|
640
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
641
|
+
let query = yield this.uploadRaw(files);
|
|
642
|
+
return yield this.formatFile(0, 0, files, query, { field });
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
updateFile(id, input = {}, { paths = [] } = {}) {
|
|
646
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
647
|
+
let query = yield file_1.FileModel.schema(this.ref, "_").findOne({ where: { id }, attributes: ['path', 'thumb'], raw: true });
|
|
648
|
+
if (!query) {
|
|
649
|
+
return Promise.reject(errors_1.generalError.NOT_FOUND);
|
|
650
|
+
}
|
|
651
|
+
if (input['path'] && input['path'] != query['path']) {
|
|
652
|
+
input['thumb'] = null;
|
|
653
|
+
[query].forEach(i => { paths = paths.concat(this.getFilePath(i)); return i['id']; });
|
|
654
|
+
}
|
|
655
|
+
if (paths.length)
|
|
656
|
+
yield this.deleteObjects(paths);
|
|
657
|
+
return (0, db_1.update)(this.ref, file_1.FileModel.getTableName().toString(), input, { where: { id } });
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
updateFolderSlug(old_slug, new_slug) {
|
|
661
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
662
|
+
if (old_slug == new_slug) {
|
|
663
|
+
return 1;
|
|
664
|
+
}
|
|
665
|
+
let exist = yield folder_1.FolderModel.schema(this.ref, "_").count({ where: { slug: new_slug } });
|
|
666
|
+
if (exist > 0) {
|
|
667
|
+
return Promise.reject(errors_1.generalError.SLUG_DUPLICATE);
|
|
668
|
+
}
|
|
669
|
+
yield folder_1.FolderModel.schema(this.ref, "_").update({ slug: new_slug }, { where: { slug: old_slug } });
|
|
670
|
+
return 1;
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
remove({ where = {}, delete_object = false, error = true, tags = [], group = undefined } = {}) {
|
|
674
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
675
|
+
where = this.beforeFindFile(where, { tags, group });
|
|
676
|
+
let files = yield file_1.FileModel.schema(this.ref, "_").findAll({ where, attributes: ['id', 'path', 'thumb'], raw: true });
|
|
677
|
+
if (files.length) {
|
|
678
|
+
let paths = [], id = files.map(i => { paths = paths.concat(this.getFilePath(i)); return i['id']; });
|
|
679
|
+
yield file_1.FileModel.schema(this.ref, "_").destroy({ where: { id } });
|
|
680
|
+
if (delete_object || config_1.config.fileDelete) {
|
|
681
|
+
yield this.deleteObjects(paths);
|
|
682
|
+
}
|
|
683
|
+
return id;
|
|
684
|
+
}
|
|
685
|
+
else if (error) {
|
|
686
|
+
return Promise.reject(errors_1.generalError.NOT_FOUND);
|
|
687
|
+
}
|
|
688
|
+
return [];
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
removeFileById(id, { tags = [], group = undefined, delete_object = false, error = true } = {}) {
|
|
692
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
693
|
+
return yield this.remove({ where: { id }, tags, group, delete_object, error });
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
removeFileByFolder(slug, { where = {}, parent_folder = "", delete_object = false, tags = [], group = undefined } = {}) {
|
|
697
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
698
|
+
if (parent_folder) {
|
|
699
|
+
let query = yield folder_1.FolderModel.schema(this.ref, "_").findOne({ where: { slug: parent_folder }, attributes: ['id'], raw: true });
|
|
700
|
+
if (!query)
|
|
701
|
+
return [];
|
|
702
|
+
where['parent_id'] = query['id'];
|
|
703
|
+
}
|
|
704
|
+
let query = yield folder_1.FolderModel.schema(this.ref, "_").findAll({ where: Object.assign({ slug }, where), attributes: ['id'], raw: true });
|
|
705
|
+
let folder_id = query.map(i => i['id']);
|
|
706
|
+
if (folder_id.length) {
|
|
707
|
+
let where_file = { folder_id };
|
|
708
|
+
return yield this.remove({ where: where_file, tags, group, delete_object, error: false });
|
|
709
|
+
}
|
|
710
|
+
return [];
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
removeFolder(index, { where = {}, field = "slug", parent_folder = "", delete_object = false } = {}) {
|
|
714
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
715
|
+
try {
|
|
716
|
+
if (parent_folder) {
|
|
717
|
+
let query = yield folder_1.FolderModel.schema(this.ref, "_").findOne({ where: { slug: parent_folder }, attributes: ['id'], raw: true });
|
|
718
|
+
if (!query)
|
|
719
|
+
return [];
|
|
720
|
+
where['parent_id'] = query['id'];
|
|
721
|
+
}
|
|
722
|
+
let query = yield folder_1.FolderModel.schema(this.ref, "_").findAll({ where: Object.assign({ [field]: index }, where), attributes: ['id'], raw: true });
|
|
723
|
+
let folder_id = query.map(i => i['id']);
|
|
724
|
+
if (folder_id.length) {
|
|
725
|
+
yield this.remove({ where: { folder_id }, delete_object, error: false });
|
|
726
|
+
yield folder_1.FolderModel.schema(this.ref, "_").destroy({ where: { id: folder_id } });
|
|
727
|
+
}
|
|
728
|
+
return folder_id;
|
|
729
|
+
}
|
|
730
|
+
catch (err) {
|
|
731
|
+
return [];
|
|
732
|
+
}
|
|
733
|
+
});
|
|
734
|
+
}
|
|
735
|
+
deleteObjects(paths, { origin = "" } = {}) {
|
|
736
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
737
|
+
let storage = yield this.getStorage();
|
|
738
|
+
if (storage['driver'] == "object") {
|
|
739
|
+
origin = origin || this.url;
|
|
740
|
+
let Objects = [];
|
|
741
|
+
paths.forEach(path => {
|
|
742
|
+
if (path) {
|
|
743
|
+
let Key = path.split(origin)[1];
|
|
744
|
+
Key && Objects.push({ Key });
|
|
745
|
+
}
|
|
746
|
+
});
|
|
747
|
+
if (!Objects.length) {
|
|
748
|
+
return null;
|
|
749
|
+
}
|
|
750
|
+
return yield this.s3.deleteObjects({
|
|
751
|
+
Bucket: this.bucket,
|
|
752
|
+
Delete: { Objects },
|
|
753
|
+
}).promise();
|
|
754
|
+
}
|
|
755
|
+
else {
|
|
756
|
+
let result = { error: 0, n: 0 };
|
|
757
|
+
for (let filename of paths) {
|
|
758
|
+
try {
|
|
759
|
+
let destination = path.join(config_1.config.fileDestination, filename);
|
|
760
|
+
yield fs.unlinkSync(destination);
|
|
761
|
+
result['n'] += 1;
|
|
762
|
+
}
|
|
763
|
+
catch (err) {
|
|
764
|
+
result['error'] += 1;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
return result;
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
exports.FileUtil = FileUtil;
|