| 56 | |
| 57 | @post('filename', Types.Filename) |
| 58 | async postUploadFile({ }, filename: string) { |
| 59 | this.checkPriv(PRIV.PRIV_CREATE_FILE); |
| 60 | if ((this.user._files?.length || 0) >= system.get('limit.user_files')) { |
| 61 | if (!this.user.hasPriv(PRIV.PRIV_UNLIMITED_QUOTA)) throw new FileLimitExceededError('count'); |
| 62 | } |
| 63 | const file = this.request.files?.file; |
| 64 | if (!file) throw new ValidationError('file'); |
| 65 | const size = Math.sum((this.user._files || []).map((i) => i.size)) + file.size; |
| 66 | if (size >= system.get('limit.user_files_size')) { |
| 67 | if (!this.user.hasPriv(PRIV.PRIV_UNLIMITED_QUOTA)) throw new FileLimitExceededError('size'); |
| 68 | } |
| 69 | if (this.user._files.find((i) => i.name === filename)) throw new FileExistsError(filename); |
| 70 | await storage.put(`user/${this.user._id}/${filename}`, file.filepath, this.user._id); |
| 71 | const meta = await storage.getMeta(`user/${this.user._id}/${filename}`); |
| 72 | const payload = { name: filename, ...pick(meta, ['size', 'lastModified', 'etag']) }; |
| 73 | if (!meta) throw new FileUploadError(); |
| 74 | this.user._files.push({ _id: filename, ...payload }); |
| 75 | await user.setById(this.user._id, { _files: this.user._files }); |
| 76 | this.back(); |
| 77 | } |
| 78 | |
| 79 | @post('files', Types.ArrayOf(Types.Filename)) |
| 80 | async postDeleteFiles({ }, files: string[]) { |