diff --git a/admin-imgtc.html b/admin-imgtc.html
index e8fed243c..d8e64d7aa 100644
--- a/admin-imgtc.html
+++ b/admin-imgtc.html
@@ -104,9 +104,10 @@
+
@@ -333,7 +315,7 @@
// 搜索匹配
const searchLower = this.search.toLowerCase();
const matchesSearch = !searchLower || [
- (data.metadata.originalName || '').toLowerCase(),
+ (data.metadata.fileName || '').toLowerCase(),
data.name?.toLowerCase(),
].some(field => field?.includes(searchLower));
@@ -393,16 +375,17 @@
sort(command) { this.sortOption = command; }, // 切换排序方式
filter(command) { this.filterOption = command; }, // 切换筛选方式
sortData(data) {
- return this.sortOption === 'nameAsc' ? data.sort((a, b) => a.name.localeCompare(b.name)) :
+ return this.sortOption === 'nameAsc' ? data.sort((a, b) =>
+ (a.metadata.fileName || a.name).localeCompare(b.metadata.fileName || b.name)) :
data.sort((a, b) => b.metadata.TimeStamp - a.metadata.TimeStamp);
},
formattedFileDetails(item) {
const metadata = item.metadata;
- const timestamp = new Date(metadata.TimeStamp).toLocaleString();
+ const timestamp = new Date(metadata.TimeStamp).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
return `
-
当前文件名:${item.name}
-
原始文件名:${metadata.originalName || '未知'}
+
ID:${item.name}
+
文件名:${metadata.fileName || item.name}
上传时间:${timestamp}
元数据:${JSON.stringify(metadata)}
@@ -794,6 +777,33 @@
getFileType(filename) {
const ext = filename.split('.').pop();
return `${ext.toUpperCase()}`;
+ },
+ handleEditName(item) {
+ this.$prompt('', '修改文件名', {
+ inputValue: item.metadata?.fileName || item.name,
+ confirmButtonText: '确定',
+ cancelButtonText: '取消',
+ inputValidator: (value) => {
+ if (!value) return '文件名不能为空';
+ if (value.length > 64) return '文件名不能超过64个字符';
+ return true;
+ }
+ }).then(({ value }) => {
+ fetch(`./api/manage/editName/${item.name}?newName=${encodeURIComponent(value)}`, {
+ method: 'GET',
+ credentials: 'include'
+ })
+ .then(response => response.json())
+ .then(result => {
+ if (result.success) {
+ item.metadata.fileName = value;
+ this.$message.success('文件名修改成功');
+ } else {
+ this.$message.error('文件名修改失败');
+ }
+ })
+ .catch(() => this.$message.error('修改文件名时出错,请检查网络连接'));
+ }).catch(() => {});
}
},
mounted() {
diff --git a/functions/api/manage/editName/[id].js b/functions/api/manage/editName/[id].js
new file mode 100644
index 000000000..e2e0225bc
--- /dev/null
+++ b/functions/api/manage/editName/[id].js
@@ -0,0 +1,22 @@
+export async function onRequest(context) {
+ const { params, env } = context;
+
+ console.log("Request ID:", params.id);
+
+ // 获取元数据
+ const value = await env.img_url.getWithMetadata(params.id);
+ console.log("Current metadata:", value);
+
+ // 如果记录不存在
+ if (!value.metadata) return new Response(`Image metadata not found for ID: ${params.id}`, { status: 404 });
+
+ // 更新文件名
+ value.metadata.fileName = params.name;
+ await env.img_url.put(params.id, "", { metadata: value.metadata });
+
+ console.log("Updated metadata:", value.metadata);
+
+ return new Response(JSON.stringify({ success: true, fileName: value.metadata.fileName }), {
+ headers: { 'Content-Type': 'application/json' },
+ });
+}
\ No newline at end of file
diff --git a/functions/file/[id].js b/functions/file/[id].js
index ab1e4b486..0b3339526 100644
--- a/functions/file/[id].js
+++ b/functions/file/[id].js
@@ -53,7 +53,7 @@ export async function onRequest(context) {
Label: record.metadata.Label || "None",
TimeStamp: record.metadata.TimeStamp || Date.now(),
liked: record.metadata.liked !== undefined ? record.metadata.liked : false,
- originalName: record.metadata.originalName || params.id, // 添加原始文件名
+ fileName: record.metadata.fileName || params.id, // 添加原始文件名
};
// Handle based on ListType and Label
@@ -72,7 +72,7 @@ export async function onRequest(context) {
} else {
// If metadata does not exist, initialize it in KV with default values
await env.img_url.put(params.id, "", {
- metadata: { ListType: "None", Label: "None", TimeStamp: Date.now(), liked: false, originalName: params.id },
+ metadata: { ListType: "None", Label: "None", TimeStamp: Date.now(), liked: false, fileName: params.id },
});
}
}
@@ -85,7 +85,7 @@ export async function onRequest(context) {
if (env.img_url) {
await env.img_url.put(params.id, "", {
- metadata: { ListType: "None", Label: moderateData.rating_label, TimeStamp: record.metadata.TimeStamp || Date.now(), liked: false, originalName: record.metadata.originalName || params.id },
+ metadata: { ListType: "None", Label: moderateData.rating_label, TimeStamp: record.metadata.TimeStamp || Date.now(), liked: false, fileName: record.metadata.fileName || params.id },
});
}
@@ -96,7 +96,7 @@ export async function onRequest(context) {
// Add image to KV with default metadata if ModerateContentApiKey is not available
console.log("KV not enabled for moderation, adding default metadata.");
await env.img_url.put(params.id, "", {
- metadata: { ListType: "None", Label: "None", TimeStamp: record.metadata.TimeStamp || Date.now(), liked: false, originalName: record.metadata.originalName || params.id },
+ metadata: { ListType: "None", Label: "None", TimeStamp: record.metadata.TimeStamp || Date.now(), liked: false, fileName: record.metadata.fileName || params.id },
});
}
}
diff --git a/functions/upload.js b/functions/upload.js
index ed3096546..5658ca34e 100644
--- a/functions/upload.js
+++ b/functions/upload.js
@@ -69,7 +69,7 @@ export async function onRequestPost(context) {
ListType: "None",
Label: "None",
liked: false,
- originalName: fileName,
+ fileName: fileName,
}
});
}