| 1005 | @post('difficulty', Types.PositiveInt, (i) => +i <= 10, true) |
| 1006 | @post('tag', Types.Content, true, null, parseCategory) |
| 1007 | async post( |
| 1008 | domainId: string, title: string, content: string, pid: string | number = '', |
| 1009 | hidden = false, difficulty = 0, tag: string[] = [], |
| 1010 | ) { |
| 1011 | if (typeof pid !== 'string') pid = `P${pid}`; |
| 1012 | if (pid && await problem.get(domainId, pid)) throw new ProblemAlreadyExistError(pid); |
| 1013 | const docId = await problem.add(domainId, pid, title, content, this.user._id, tag ?? [], { hidden, difficulty }); |
| 1014 | const files = new Set(Array.from(content.matchAll(/file:\/\/([\w-]+\.[a-zA-Z0-9]+)/g)).map((i) => i[1])); |
| 1015 | const tasks = []; |
| 1016 | for (const file of files) { |
| 1017 | if (this.user._files.find((i) => i.name === file)) { |
| 1018 | tasks.push( |
| 1019 | storage.rename(`user/${this.user._id}/${file}`, `problem/${domainId}/${docId}/additional_file/${file}`, this.user._id) |
| 1020 | .then(() => problem.addAdditionalFile(domainId, docId, file, '', this.user._id, true)), |
| 1021 | user.setById(this.user._id, { _files: this.user._files.filter((i) => i.name !== file) }), |
| 1022 | ); |
| 1023 | } |
| 1024 | } |
| 1025 | await Promise.all(tasks); |
| 1026 | this.response.body = { pid: pid || docId }; |
| 1027 | this.response.redirect = this.url('problem_files', { pid: pid || docId }); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | export const ProblemApi = { |