feat:上传文件用月份路径

This commit is contained in:
李亮亮 2024-01-15 17:01:06 +08:00
parent fc148ee2a0
commit 4876408cba
2 changed files with 14 additions and 9 deletions

View File

@ -2,7 +2,7 @@ import { router } from '../router';
import { Env } from '../[[path]]' import { Env } from '../[[path]]'
import { json } from 'itty-router-extras'; import { json } from 'itty-router-extras';
import StatusCode, { Ok, Fail, Build, ImgItem, ImgList, ImgReq, Folder, AuthToken, FailCode, NotAuth } from "../type"; 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"; import { R2ListOptions } from "@cloudflare/workers-types";
const auth = async (request: Request, env: Env) => { const auth = async (request: Request, env: Env) => {
@ -103,11 +103,11 @@ router.post('/upload', auth, async (req: Request, env: Env) => {
continue continue
} }
const time = new Date().getTime() const time = new Date().getTime()
const filename = await getFileName(fileType, time) const objecPath = await getFilePath(fileType, time)
const header = new Headers() const header = new Headers()
header.set("content-type", fileType) header.set("content-type", fileType)
header.set("content-length", `${item.size}`) 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, httpMetadata: header,
}) as R2Object }) as R2Object
if (object || object.key) { if (object || object.key) {

View File

@ -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' 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; let maxPos = baseStr.length;
const uuid = []; const uuid = [];
let q = value; let q = value;
for(;q > 0;) { for (; q > 0;) {
let mod = q % maxPos; let mod = q % maxPos;
q = (q - mod) / maxPos; q = (q - mod) / maxPos;
uuid.push(chars[mod]); uuid.push(chars[mod]);
@ -27,22 +27,27 @@ export function parseRange(encoded: string | null): undefined | { offset: number
} }
return { return {
offset: Number(parts[0]), offset: Number(parts[0]),
end: Number(parts[1]), end: Number(parts[1]),
length: Number(parts[1]) + 1 - Number(parts[0]), 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 return supportFile.indexOf(val) > -1
} }
// 获取文件名 // 获取文件名
export async function getFileName(val : string, time : number) : Promise<string> { export async function getFilePath(val: string, time: number): Promise<string> {
const types = supportFiles.filter(it => it.type === val) const types = supportFiles.filter(it => it.type === val)
if (!types || types.length < 1) { if (!types || types.length < 1) {
return val return val
} }
const rand = Math.floor(Math.random() * 100000) 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
} }