From 4876408cbac028311e3e059dd068c7a9b3e45220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BA=AE=E4=BA=AE?= Date: Mon, 15 Jan 2024 17:01:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=94=A8=E6=9C=88=E4=BB=BD=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/rest/routes/index.ts | 6 +++--- functions/rest/utils.ts | 17 +++++++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/functions/rest/routes/index.ts b/functions/rest/routes/index.ts index 980a979..45cbb96 100644 --- a/functions/rest/routes/index.ts +++ b/functions/rest/routes/index.ts @@ -2,7 +2,7 @@ import { router } from '../router'; import { Env } from '../[[path]]' import { json } from 'itty-router-extras'; import StatusCode, { Ok, Fail, Build, ImgItem, ImgList, ImgReq, Folder, AuthToken, FailCode, NotAuth } from "../type"; -import { checkFileType, getFileName, parseRange } from '../utils' +import { checkFileType, getFilePath, parseRange } from '../utils' import { R2ListOptions } from "@cloudflare/workers-types"; const auth = async (request: Request, env: Env) => { @@ -103,11 +103,11 @@ router.post('/upload', auth, async (req: Request, env: Env) => { continue } const time = new Date().getTime() - const filename = await getFileName(fileType, time) + const objecPath = await getFilePath(fileType, time) const header = new Headers() header.set("content-type", fileType) header.set("content-length", `${item.size}`) - const object = await env.PICX.put(filename, item.stream(), { + const object = await env.PICX.put(objecPath, item.stream(), { httpMetadata: header, }) as R2Object if (object || object.key) { diff --git a/functions/rest/utils.ts b/functions/rest/utils.ts index 81b5f38..50630de 100644 --- a/functions/rest/utils.ts +++ b/functions/rest/utils.ts @@ -1,4 +1,4 @@ -const supportFiles = [{type:'image/png',ext:'png'},{type:'image/jpeg',ext:'jpeg'},{type:'image/gif',ext:'gif'},{type:'image/webp',ext:'webp'},{type:'image/jpg',ext:'jpg'},{type:'image/x-icon',ext:'ico'},{type:'application/x-ico',ext:'ico'},{type:'image/vnd.microsoft.icon',ext:'ico'}] +const supportFiles = [{ type: 'image/png', ext: 'png' }, { type: 'image/jpeg', ext: 'jpeg' }, { type: 'image/gif', ext: 'gif' }, { type: 'image/webp', ext: 'webp' }, { type: 'image/jpg', ext: 'jpg' }, { type: 'image/x-icon', ext: 'ico' }, { type: 'application/x-ico', ext: 'ico' }, { type: 'image/vnd.microsoft.icon', ext: 'ico' }] const supportFile = 'image/png,image/jpeg,image/gif,image/webp,image/jpg,image/x-icon,application/x-ico,image/vnd.microsoft.icon' // 字符串编码 @@ -8,7 +8,7 @@ export function randomString(value: number) { let maxPos = baseStr.length; const uuid = []; let q = value; - for(;q > 0;) { + for (; q > 0;) { let mod = q % maxPos; q = (q - mod) / maxPos; uuid.push(chars[mod]); @@ -27,22 +27,27 @@ export function parseRange(encoded: string | null): undefined | { offset: number } return { offset: Number(parts[0]), - end: Number(parts[1]), + end: Number(parts[1]), length: Number(parts[1]) + 1 - Number(parts[0]), } } // 检查文件类是否支持 -export function checkFileType(val : string) : boolean { +export function checkFileType(val: string): boolean { return supportFile.indexOf(val) > -1 } // 获取文件名 -export async function getFileName(val : string, time : number) : Promise { +export async function getFilePath(val: string, time: number): Promise { const types = supportFiles.filter(it => it.type === val) if (!types || types.length < 1) { return val } const rand = Math.floor(Math.random() * 100000) - return randomString(time + rand).concat(`.${types[0].ext}`) + const fileName = randomString(time + rand).concat(`.${types[0].ext}`) + let date = new Date() + const year = date.getFullYear() //获取完整的年份(4位) + const month = date.getMonth() + 1 //获取当前月份(0-11,0代表1月) + return "/" + year + "/" + month + "/" + fileName + }