1 | // import multer from 'multer';
|
2 | // import fs from 'fs';
|
3 | // import nodepath from 'path';
|
4 | // import random from 'lodash/random';
|
5 | // import get from 'lodash/get';
|
6 | // import aws from 'aws-sdk';
|
7 | // import multerS3 from 'multer-s3';
|
8 | // import Api from '@lskjs/server-api';
|
9 |
|
10 | // export default class UploadApi extends Api {
|
11 | // getFileType(file) {
|
12 | // if (file && file.originalname) {
|
13 | // const res = file.originalname.match(/\.([0-9a-z]+)(?:[\?#]|$)/i); //eslint-disable-line
|
14 | // if (res && res[1]) {
|
15 | // return res[1];
|
16 | // }
|
17 | // }
|
18 | // return null;
|
19 | // }
|
20 | // // @TODO real file name
|
21 | // // const getFileName = (file) => {
|
22 | // // return file.originalname;
|
23 | // // };
|
24 |
|
25 | // createDir(targetDir) {
|
26 | // if (!fs.existsSync(targetDir)) {
|
27 | // targetDir.split('/').forEach((dir, index, splits) => {
|
28 | // const parent = splits.slice(0, index).join('/');
|
29 | // const dirPath = nodepath.resolve(parent, dir);
|
30 | // if (!fs.existsSync(dirPath)) {
|
31 | // fs.mkdirSync(dirPath);
|
32 | // }
|
33 | // });
|
34 | // }
|
35 | // }
|
36 |
|
37 | // getFilePath(req, file) {
|
38 | // const { e403 } = this.app.errors;
|
39 | // const { config } = this;
|
40 | // let { path = 'storage' } = config;
|
41 |
|
42 | // if (req.user && req.user._id) {
|
43 | // path += `/${req.user._id}`;
|
44 | // } else if (config.allowGuest) {
|
45 | // path += '/general';
|
46 | // } else {
|
47 | // throw e403('Guest can not upload files');
|
48 | // }
|
49 |
|
50 | // let fileName;
|
51 | // if (config.allowSetFilename) {
|
52 | // fileName = file.originalname;
|
53 | // } else {
|
54 | // fileName = `${Date.now()}_${random(0, 1000)}.${this.getFileType(file)}`;
|
55 | // }
|
56 | // const { prefix = '' } = config;
|
57 | // const path2 = `${path}/${prefix}${fileName}`;
|
58 | // // console.log({ path2 });
|
59 |
|
60 | // return path2;
|
61 | // }
|
62 |
|
63 | // getS3Storage() {
|
64 | // return multerS3({
|
65 | // s3: this.s3,
|
66 | // bucket: this.config.s3.bucket,
|
67 | // contentType: multerS3.AUTO_CONTENT_TYPE,
|
68 | // acl: 'public-read',
|
69 | // key3: (req, file, cb) => {
|
70 | // const filename = nodepath.parse(file.originalname);
|
71 | // // console.log({ req, file });
|
72 | // // console.log(`avatar_${req.user._id}.${filename.ext}`);
|
73 | // cb(null, `avatar_${req.user._id}.${filename.ext}`);
|
74 | // },
|
75 | // key: async (req, file, cb) => {
|
76 | // let filename;
|
77 | // try {
|
78 | // // console.log('req, file', file);
|
79 | // filename = this.getFilePath(req, file);
|
80 | // // console.log('filename', filename);
|
81 | // filename = filename.replace(/\//g, '__');
|
82 | // // console.log('filename2', filename);
|
83 | // } catch (err) {
|
84 | // return cb(err);
|
85 | // }
|
86 | // return cb(null, filename);
|
87 | // },
|
88 | // });
|
89 | // }
|
90 |
|
91 | // getDiskStorage() {
|
92 | // // const config = this.app.config.upload;
|
93 | // const storage = multer.diskStorage({
|
94 | // destination: (req, file, cb) => {
|
95 | // // console.log('destination');
|
96 | // let path;
|
97 | // let dirname;
|
98 | // try {
|
99 | // path = this.getFilePath(req, file);
|
100 | // dirname = path.split('/').slice(0, -1).join('/');
|
101 | // this.createDir(dirname);
|
102 | // } catch (err) {
|
103 | // return cb(err);
|
104 | // }
|
105 | // return cb(null, dirname);
|
106 | // },
|
107 | // filename: (req, file, cb) => {
|
108 | // let path;
|
109 | // let filename;
|
110 | // try {
|
111 | // path = this.getFilePath(req, file);
|
112 | // [filename] = path.split('/').reverse();
|
113 | // } catch (err) {
|
114 | // return cb(err);
|
115 | // }
|
116 | // // console.log('filename', filename);
|
117 | // // this.createDir(filename);
|
118 | // return cb(null, filename);
|
119 | // },
|
120 | // });
|
121 | // return storage;
|
122 | // }
|
123 |
|
124 | // getMulter() {
|
125 | // const { e400 } = this.app.errors;
|
126 | // const config = this.app.config.upload;
|
127 | // const fileFilter = (req, file, cb) => {
|
128 | // if (Array.isArray(config.mimetypes)) {
|
129 | // if (config.mimetypes.indexOf(file.mimetype) === -1) {
|
130 | // return cb(e400('You are not allowed to upload files with this extension'));
|
131 | // }
|
132 | // }
|
133 | // return cb(null, true);
|
134 | // };
|
135 |
|
136 | // const limits = {};
|
137 | // if (config.maxSize) {
|
138 | // const fileSize = parseFloat(config.maxSize) * 1024 * 1024;
|
139 | // limits.fileSize = fileSize;
|
140 | // }
|
141 | // // console.log('getMulter', {
|
142 | // // storage: this.storage,
|
143 | // // limits,
|
144 | // // fileFilter,
|
145 | // // });
|
146 | // return multer({
|
147 | // storage: this.storage,
|
148 | // limits,
|
149 | // fileFilter,
|
150 | // });
|
151 | // }
|
152 |
|
153 | // getApi() {
|
154 | // const config = this.app.config.upload;
|
155 | // function processFile(file) {
|
156 | // if (config.s3) {
|
157 | // return {
|
158 | // name: file.fieldname,
|
159 | // url: file.location,
|
160 | // path: file.location,
|
161 | // relative: `/${file.key}`,
|
162 | // mimetype: file.contentType,
|
163 | // filename: file.originalname,
|
164 | // };
|
165 | // }
|
166 |
|
167 | // return {
|
168 | // name: file.fieldname,
|
169 | // url: `${config.url}/${file.path}`,
|
170 | // path: `/${file.path}`,
|
171 | // relative: `/${file.path}`,
|
172 | // mimetype: file.mimetype,
|
173 | // filename: file.originalname,
|
174 | // };
|
175 | // }
|
176 |
|
177 | // const api = this.app.asyncRouter();
|
178 | // api.post('/many', this.multer.any(), async (req) => {
|
179 | // const { files = [] } = req;
|
180 | // return files.map(processFile);
|
181 | // });
|
182 |
|
183 | // // const upload = multer();
|
184 | // // api.post('/', this.multer.single('file'), async (req) => {
|
185 | // api.post('/', this.multer.single('file'), async (req) => {
|
186 | // const { file } = req;
|
187 | // if (!file) {
|
188 | // throw new Err('!file');
|
189 | // }
|
190 | // return processFile(file);
|
191 | // });
|
192 | // return api;
|
193 | // }
|
194 |
|
195 | // async run() {
|
196 | // this.app.app.use('/api/module/upload', this.getApi());
|
197 | // }
|
198 | // }
|
199 | ;
|
200 | //# sourceMappingURL=_UploadApi.js.map |
\ | No newline at end of file |