From 9452df4203f8e4c2316192969ad49395eea97a75 Mon Sep 17 00:00:00 2001 From: undefined Date: Tue, 28 Oct 2025 16:06:43 +0800 Subject: [PATCH] core&ui: fix homework_files --- packages/hydrooj/src/handler/homework.ts | 16 ++++++++-------- packages/hydrooj/src/upgrade.ts | 15 ++++++++++++--- packages/ui-default/build/main.ts | 1 - .../ui-default/templates/contest_manage.html | 2 +- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/hydrooj/src/handler/homework.ts b/packages/hydrooj/src/handler/homework.ts index 6c66b5d40..f1aaeed33 100644 --- a/packages/hydrooj/src/handler/homework.ts +++ b/packages/hydrooj/src/handler/homework.ts @@ -109,8 +109,8 @@ class HomeworkDetailHandler extends Handler { tdoc: this.tdoc, tsdoc, udict, ddocs, page, dpcount, dcount, }; this.response.body.tdoc.content = this.response.body.tdoc.content - .replace(/\(file:\/\//g, `(./${this.tdoc.docId}/file/`) - .replace(/="file:\/\//g, `="./${this.tdoc.docId}/file/`); + .replace(/\(file:\/\//g, `(./${this.tdoc.docId}/file/public/`) + .replace(/="file:\/\//g, `="./${this.tdoc.docId}/file/public/`); if ( (contest.isNotStarted(this.tdoc) || (!tsdoc?.attend && !contest.isDone(this.tdoc))) && !this.user.own(this.tdoc) @@ -247,7 +247,7 @@ class HomeworkEditHandler extends Handler { await Promise.all([ record.updateMulti(domainId, { domainId, contest: tid }, undefined, undefined, { contest: '' }), contest.del(domainId, tid), - storage.del(tdoc.files?.map((i) => `contest/${domainId}/${tid}/private/${i.name}`) || [], this.user._id), + storage.del(tdoc.files?.map((i) => `contest/${domainId}/${tid}/public/${i.name}`) || [], this.user._id), ]); this.response.redirect = this.url('homework_main'); } @@ -271,7 +271,7 @@ export class HomeworkFilesHandler extends Handler { tsdoc: await contest.getStatus(domainId, this.tdoc.docId, this.user._id), udoc: await user.getById(domainId, this.tdoc.owner), files: sortFiles(this.tdoc.files || []), - urlForFile: (filename: string) => this.url('homework_file_download', { tid, filename }), + urlForFile: (filename: string) => this.url('homework_file_download', { tid, filename, type: 'public' }), }; this.response.pjax = 'partials/files.html'; this.response.template = 'homework_files.html'; @@ -289,8 +289,8 @@ export class HomeworkFilesHandler extends Handler { if (size >= system.get('limit.contest_files_size')) { throw new FileLimitExceededError('size'); } - await storage.put(`contest/${domainId}/${tid}/private/${filename}`, file.filepath, this.user._id); - const meta = await storage.getMeta(`contest/${domainId}/${tid}/private/${filename}`); + await storage.put(`contest/${domainId}/${tid}/public/${filename}`, file.filepath, this.user._id); + const meta = await storage.getMeta(`contest/${domainId}/${tid}/public/${filename}`); const payload = { _id: filename, name: filename, ...pick(meta, ['size', 'lastModified', 'etag']) }; if (!meta) throw new FileUploadError(); await contest.edit(domainId, tid, { files: [...(this.tdoc.files || []), payload] }); @@ -301,7 +301,7 @@ export class HomeworkFilesHandler extends Handler { @post('files', Types.ArrayOf(Types.Filename)) async postDeleteFiles(domainId: string, tid: ObjectId, files: string[]) { await Promise.all([ - storage.del(files.map((t) => `contest/${domainId}/${tid}/private/${t}`), this.user._id), + storage.del(files.map((t) => `contest/${domainId}/${tid}/public/${t}`), this.user._id), contest.edit(domainId, tid, { files: this.tdoc.files.filter((i) => !files.includes(i.name)) }), ]); this.back(); @@ -315,7 +315,7 @@ export async function apply(ctx) { ctx.Route('homework_code', '/homework/:tid/code', ContestCodeHandler, PERM.PERM_VIEW_HOMEWORK); ctx.Route('homework_edit', '/homework/:tid/edit', HomeworkEditHandler); ctx.Route('homework_files', '/homework/:tid/file', HomeworkFilesHandler, PERM.PERM_VIEW_HOMEWORK); - ctx.Route('homework_file_download', '/homework/:tid/file/:filename', ContestFileDownloadHandler, PERM.PERM_VIEW_HOMEWORK); + ctx.Route('homework_file_download', '/homework/:tid/file/:type/:filename', ContestFileDownloadHandler, PERM.PERM_VIEW_HOMEWORK); await ctx.inject(['scoreboard'], ({ Route }) => { Route('homework_scoreboard', '/homework/:tid/scoreboard', ContestScoreboardHandler, PERM.PERM_VIEW_HOMEWORK_SCOREBOARD); Route('homework_scoreboard_view', '/homework/:tid/scoreboard/:view', ContestScoreboardHandler, PERM.PERM_VIEW_HOMEWORK_SCOREBOARD); diff --git a/packages/hydrooj/src/upgrade.ts b/packages/hydrooj/src/upgrade.ts index 56be3bd0b..0992d9399 100644 --- a/packages/hydrooj/src/upgrade.ts +++ b/packages/hydrooj/src/upgrade.ts @@ -617,12 +617,21 @@ export const coreScripts: MigrationScript[] = [ await discussion.coll.deleteMany({ content: { $not: { $type: 'string' } } }); return true; }, - async function _95_96() { + null, + async function _96_97() { const files = await StorageModel.list('contest/', true); + const rename = async (path: string, newPath: string) => { + if (path === newPath) return; + console.log('Rename', path, '->', newPath); + await StorageModel.rename(path, newPath); + }; for (const file of files) { const [, domainId, tid, type, name] = file.path.split('/'); - if (!name) await StorageModel.rename(file.path, `contest/${domainId}/${tid}/public/${type}`); + const tdoc = await contest.get(domainId, new ObjectId(tid)); + if (!tdoc) console.error('Contest not found', file.path); + if (tdoc.rule === 'homework' || !name) { + await rename(file.path, `contest/${domainId}/${tid}/public/${name || type}`); + } } - return true; }, ]; diff --git a/packages/ui-default/build/main.ts b/packages/ui-default/build/main.ts index cdffc40cd..403ebeb46 100644 --- a/packages/ui-default/build/main.ts +++ b/packages/ui-default/build/main.ts @@ -6,7 +6,6 @@ import fs from 'fs-extra'; import { globbySync } from 'globby'; import webpack, { Stats } from 'webpack'; import WebpackDevServer from 'webpack-dev-server'; -import pkg from '../package.json'; import webpackConfig from './config/webpack'; import root from './utils/root'; diff --git a/packages/ui-default/templates/contest_manage.html b/packages/ui-default/templates/contest_manage.html index 4085abbe8..0f05a7a1a 100644 --- a/packages/ui-default/templates/contest_manage.html +++ b/packages/ui-default/templates/contest_manage.html @@ -63,7 +63,7 @@ - {% set files = tdoc.privateFiles %} + {% set files = privateFiles %} {% set filetype = "private" %} {% include "partials/files.html" %}