Skip to content

Commit fd0536f

Browse files
committed
feat: #4 Feature - File information editing
1 parent 5c25809 commit fd0536f

File tree

4 files changed

+79
-18
lines changed

4 files changed

+79
-18
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ MYSQL_PORT=3306
8686
CREATE TABLE files (
8787
id VARCHAR(50) DEFAULT NULL,
8888
filename VARCHAR(255) NOT NULL,
89-
filesize BIGINT(20) NOT NULL,
90-
filelocation VARCHAR(255) NOT NULL,
89+
file_size BIGINT(20) NOT NULL,
90+
file_location VARCHAR(255) NOT NULL,
9191
created_by VARCHAR(255) NOT NULL,
9292
created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
9393
updated_by VARCHAR(255) DEFAULT NULL,
@@ -109,8 +109,8 @@ CREATE TABLE files (
109109
|------------------|----------------|----------|----------------------|----------------------------------------------|
110110
| `id` | int(11) | NOT NULL | | 文件的唯一标识 |
111111
| `filename` | varchar(255) | NOT NULL | | 文件名 |
112-
| `filesize` | bigint(20) | NOT NULL | | 文件大小(以字节为单位) |
113-
| `filelocation` | varchar(255) | NOT NULL | | 文件存储的位置 |
112+
| `file_size` | bigint(20) | NOT NULL | | 文件大小(以字节为单位) |
113+
| `file_location` | varchar(255) | NOT NULL | | 文件存储的位置 |
114114
| `created_by` | varchar(255) | NOT NULL | | 创建该文件的用户 |
115115
| `created_at` | timestamp | NULL | CURRENT_TIMESTAMP | 文件的创建时间,默认当前时间 |
116116
| `updated_by` | varchar(255) | NULL | NULL | 最近更新该文件的用户 |

models/files.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { DataTypes } = require('sequelize');
22
const sequelize = require('../utils/dbInstance'); // 修改为实际的sequelize实例路径
3-
const File = sequelize.define('File', {
3+
const Files = sequelize.define('Files', {
44
id: {
55
type: DataTypes.STRING(50),
66
allowNull: false, // 必须为 NOT NULL
@@ -10,11 +10,11 @@ const File = sequelize.define('File', {
1010
type: DataTypes.STRING(255),
1111
allowNull: false,
1212
},
13-
filesize: {
13+
file_size: {
1414
type: DataTypes.BIGINT,
1515
allowNull: false,
1616
},
17-
filelocation: {
17+
file_location: {
1818
type: DataTypes.STRING(255),
1919
allowNull: false,
2020
},
@@ -96,4 +96,4 @@ const File = sequelize.define('File', {
9696
collate: 'utf8mb4_general_ci',
9797
});
9898

99-
module.exports = File;
99+
module.exports = Files;

public/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
const { total, items } = await response.json();
8686
images.value = items.map(file => ({
8787
...file,
88-
preview: `${file.thumb_location ?? file.filelocation}`
88+
preview: `${file.thumb_location ?? file.file_location}`
8989
}));
9090
imageTotal.value = total;
9191
} catch (error) {
@@ -215,7 +215,7 @@
215215
</div>
216216
<div class="waterfall">
217217
<p class="waterfall-item" v-for="(image, index) in images" :key="index">
218-
<a target="_blank" :href="image.filelocation">
218+
<a target="_blank" :href="image.file_location">
219219
<el-image :src="image.preview" :alt="image.filename" fit="cover" />
220220
</a>
221221
</p>

routers/files.js

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ router.post("/files", async (ctx) => {
8989
await Files.create({
9090
id: fileId,
9191
filename: path.basename(realFilePath),
92-
filesize: (await fsp.stat(realFilePath)).size,
93-
filelocation: fileUrl,
92+
file_size: (await fsp.stat(realFilePath)).size,
93+
file_location: fileUrl,
9494
real_file_location: realFilePath,
9595
created_by: ctx.query.createdBy || "anonymous",
9696
is_public: isFilePublic,
@@ -179,9 +179,9 @@ router.get("/files", async (ctx) => {
179179
"public_expiration",
180180
"updated_at",
181181
"updated_by",
182-
"filesize",
182+
"file_size",
183183
"filename",
184-
"filelocation",
184+
"file_location",
185185
"thumb_location",
186186
"is_public",
187187
],
@@ -216,12 +216,11 @@ router.get("/files/:id", async (ctx) => {
216216
attributes: [
217217
"id",
218218
"filename",
219-
"is_delete",
220219
"is_public",
221220
"public_expiration",
222221
"is_thumb",
223-
"filesize",
224-
"filelocation",
222+
"file_size",
223+
"file_location",
225224
"thumb_location",
226225
"mime",
227226
"ext",
@@ -248,6 +247,69 @@ router.get("/files/:id", async (ctx) => {
248247
}
249248
});
250249

250+
// 编辑文件信息接口
251+
router.put('/files/:id', async (ctx) => {
252+
const { id } = ctx.params;
253+
const {
254+
filename,
255+
is_public,
256+
updated_by,
257+
public_expiration,
258+
public_by,
259+
} = ctx.request.body;
260+
261+
try {
262+
// 查找文件
263+
const file = await Files.findOne({
264+
where: {
265+
id,
266+
is_delete: false,
267+
}
268+
});
269+
270+
if (!file) {
271+
ctx.status = 404;
272+
ctx.body = { message: 'File not found' };
273+
return;
274+
}
275+
276+
// 更新文件信息
277+
await file.update({
278+
filename,
279+
is_public,
280+
updated_by,
281+
updated_at: new Date(),
282+
public_expiration,
283+
public_by,
284+
});
285+
286+
const updatedFile = {
287+
id: file.id,
288+
filename: file.filename,
289+
is_public: file.is_public,
290+
public_expiration: file.public_expiration,
291+
is_thumb: file.is_thumb,
292+
file_size: file.file_size,
293+
file_location: file.file_location,
294+
thumb_location: file.thumb_location,
295+
mime: file.mime,
296+
ext: file.ext,
297+
created_at: file.created_at,
298+
created_by: file.created_by,
299+
updated_at: file.updated_at,
300+
updated_by: file.updated_by,
301+
};
302+
303+
// 返回更新后的文件信息
304+
ctx.body = updatedFile;
305+
} catch (error) {
306+
ctx.status = 500;
307+
ctx.body = { message: 'Error updating file information', error: error.message };
308+
console.error('Update file error:', error);
309+
}
310+
});
311+
312+
251313
// 文件预览
252314
router.get("/files/:id/preview", async (ctx) => {
253315
const { id } = ctx.params;
@@ -266,7 +328,6 @@ router.get("/files/:id/preview", async (ctx) => {
266328
},
267329
attributes: [
268330
"filename",
269-
"is_delete",
270331
"is_public",
271332
"public_expiration",
272333
"real_file_location",

0 commit comments

Comments
 (0)