-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: upload support auto clean and whitelist (#1484)
- Loading branch information
Showing
17 changed files
with
328 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,39 @@ | ||
import { UploadOptions } from './dist/index'; | ||
import { UploadFileInfo, UploadOptions } from './dist/index'; | ||
export * from './dist/index'; | ||
|
||
declare module '@midwayjs/core/dist/interface' { | ||
interface MidwayConfig { | ||
upload: Partial<UploadOptions>; | ||
} | ||
} | ||
declare module '@midwayjs/koa/dist/interface' { | ||
interface Context { | ||
files?: UploadFileInfo<any>[]; | ||
fields?: { [fieldName: string]: any }; | ||
cleanupRequestFiles?: () => Promise<Array<boolean>>; | ||
} | ||
} | ||
|
||
declare module '@midwayjs/web/dist/interface' { | ||
interface Context { | ||
files?: UploadFileInfo<any>[]; | ||
fields?: { [fieldName: string]: any }; | ||
cleanupRequestFiles?: () => Promise<Array<boolean>>; | ||
} | ||
} | ||
|
||
declare module '@midwayjs/faas/dist/interface' { | ||
interface Context { | ||
files?: UploadFileInfo<any>[]; | ||
fields?: { [fieldName: string]: any }; | ||
cleanupRequestFiles?: () => Promise<Array<boolean>>; | ||
} | ||
} | ||
|
||
declare module '@midwayjs/express/dist/interface' { | ||
interface Context { | ||
files?: UploadFileInfo<any>[]; | ||
fields?: { [fieldName: string]: any }; | ||
cleanupRequestFiles?: () => Promise<Array<boolean>>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,12 @@ | ||
import { UploadOptions } from '../interface'; | ||
import { join } from 'path'; | ||
import { tmpdir } from 'os'; | ||
import { uploadWhiteList } from '../constants'; | ||
|
||
export const upload: UploadOptions = { | ||
mode: 'file', | ||
fileSize: '10mb', | ||
whitelist: [ | ||
// images | ||
'.jpg', | ||
'.jpeg', // image/jpeg | ||
'.png', // image/png, image/x-png | ||
'.gif', // image/gif | ||
'.bmp', // image/bmp | ||
'.wbmp', // image/vnd.wap.wbmp | ||
'.webp', | ||
'.tif', | ||
'.psd', | ||
// text | ||
'.svg', | ||
'.js', | ||
'.jsx', | ||
'.json', | ||
'.css', | ||
'.less', | ||
'.html', | ||
'.htm', | ||
'.xml', | ||
'.pdf', | ||
// tar | ||
'.zip', | ||
'.gz', | ||
'.tgz', | ||
'.gzip', | ||
// video | ||
'.mp3', | ||
'.mp4', | ||
'.avi', | ||
], | ||
whitelist: uploadWhiteList, | ||
tmpdir: join(tmpdir(), 'midway-upload-files'), | ||
cleanTimeout: 5 * 60 * 1000, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export const uploadWhiteList = [ | ||
// images | ||
'.jpg', | ||
'.jpeg', // image/jpeg | ||
'.png', // image/png, image/x-png | ||
'.gif', // image/gif | ||
'.bmp', // image/bmp | ||
'.wbmp', // image/vnd.wap.wbmp | ||
'.webp', | ||
'.tif', | ||
'.psd', | ||
// text | ||
'.svg', | ||
'.js', | ||
'.jsx', | ||
'.json', | ||
'.css', | ||
'.less', | ||
'.html', | ||
'.htm', | ||
'.xml', | ||
'.pdf', | ||
// tar | ||
'.zip', | ||
'.gz', | ||
'.tgz', | ||
'.gzip', | ||
// video | ||
'.mp3', | ||
'.mp4', | ||
'.avi', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { promises, constants } from 'fs'; | ||
import { join } from 'path'; | ||
const { readdir, access, stat, unlink, mkdir } = promises; | ||
let autoRemoveUploadTmpFileTimeoutHandler; | ||
let autoRemoveUploadTmpFilePromise; | ||
export const autoRemoveUploadTmpFile = async ( | ||
tmpDir: string, | ||
cleanTimeout: number | ||
) => { | ||
clearTimeout(autoRemoveUploadTmpFileTimeoutHandler); | ||
let waitTime = cleanTimeout / 3; | ||
if (waitTime < 1000) { | ||
waitTime = 1000; | ||
} | ||
|
||
if (autoRemoveUploadTmpFilePromise) { | ||
const exists = await checkExists(tmpDir); | ||
if (exists) { | ||
const paths = await readdir(tmpDir); | ||
const now = Date.now(); | ||
await Promise.all( | ||
paths.map(async path => { | ||
const filePath = join(tmpDir, path); | ||
try { | ||
const statInfo = await stat(filePath); | ||
if (statInfo.isFile() && now - statInfo.ctimeMs > cleanTimeout) { | ||
await unlink(filePath); | ||
} | ||
} catch { | ||
return false; | ||
} | ||
}) | ||
); | ||
} | ||
} | ||
|
||
autoRemoveUploadTmpFileTimeoutHandler = setTimeout(() => { | ||
autoRemoveUploadTmpFilePromise = autoRemoveUploadTmpFile( | ||
tmpDir, | ||
cleanTimeout | ||
); | ||
}, waitTime); | ||
}; | ||
|
||
export const stopAutoRemoveUploadTmpFile = async () => { | ||
if (autoRemoveUploadTmpFilePromise) { | ||
await autoRemoveUploadTmpFilePromise; | ||
autoRemoveUploadTmpFilePromise = null; | ||
} | ||
clearTimeout(autoRemoveUploadTmpFileTimeoutHandler); | ||
}; | ||
|
||
export const checkExists = async (path: string): Promise<boolean> => { | ||
try { | ||
await access(path, constants.W_OK | constants.R_OK); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
}; | ||
|
||
export const ensureDir = async (dirPath: string): Promise<boolean> => { | ||
const isExists = await checkExists(dirPath); | ||
if (isExists) { | ||
return true; | ||
} | ||
try { | ||
await mkdir(dirPath, { recursive: true }); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
}; |
Oops, something went wrong.