Merge pull request #286 from shadowhenry/main

bugfix: Compatible with different browsers for uploading zip mimetype
pull/287/head
北野 - Michael Li 2 years ago committed by GitHub
commit 5be3ea3a18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -163,7 +163,7 @@ func getFileExt(s string) (string, mir.Error) {
return ".mp4", nil return ".mp4", nil
case "video/quicktime": case "video/quicktime":
return ".mov", nil return ".mov", nil
case "application/zip": case "application/zip", "application/x-zip", "application/octet-stream", "application/x-zip-compressed":
return ".zip", nil return ".zip", nil
default: default:
return "", _errFileInvalidExt.WithDetails("仅允许 png/jpg/gif/mp4/mov/zip 类型") return "", _errFileInvalidExt.WithDetails("仅允许 png/jpg/gif/mp4/mov/zip 类型")

@ -278,6 +278,7 @@ import {
} from '@vicons/ionicons5'; } from '@vicons/ionicons5';
import { createPost } from '@/api/post'; import { createPost } from '@/api/post';
import { parsePostTag } from '@/utils/content'; import { parsePostTag } from '@/utils/content';
import { isZipFile } from '@/utils/isZipFile';
import type { MentionOption, UploadFileInfo, UploadInst } from 'naive-ui'; import type { MentionOption, UploadFileInfo, UploadInst } from 'naive-ui';
import { VisibilityEnum, PostItemTypeEnum } from '@/utils/IEnum'; import { VisibilityEnum, PostItemTypeEnum } from '@/utils/IEnum';
@ -432,11 +433,9 @@ const beforeUpload = async (data: any) => {
window.$message.warning('100MB'); window.$message.warning('100MB');
return false; return false;
} }
// 附件类型校验 // 附件类型校验
if ( if (
uploadType.value === 'attachment' && uploadType.value === 'attachment' && !(await isZipFile(data.file.file))
!['application/zip'].includes(data.file.file?.type)
) { ) {
window.$message.warning(' zip '); window.$message.warning(' zip ');
return false; return false;

@ -0,0 +1,47 @@
export const isZipFile = (file: File): Promise<unknown> => {
const fileReader = new FileReader();
const isValidZipFileType = (fileType: string): boolean => {
const zipFileTypes = ['application/zip', 'application/x-zip', 'application/octet-stream', 'application/x-zip-compressed'];
return zipFileTypes.includes(fileType);
};
const checkFileType = (): boolean => {
const arr = new Uint8Array(fileReader.result as ArrayBuffer).subarray(0, 4);
let header = '';
for (let i = 0; i < arr.length; i++) {
header += arr[i].toString(16);
}
switch (header) {
case '504b0304':
case '504b0506':
case '504b0708':
return isValidZipFileType('application/zip');
case '504b030414':
return isValidZipFileType('application/x-zip-compressed');
case '504b0508':
return isValidZipFileType('application/x-zip');
case '504b5370':
return isValidZipFileType('application/octet-stream');
default:
return false;
}
};
return new Promise((resolve, reject) => {
fileReader.onloadend = () => {
const fileType = file.type;
if (fileType === '' || fileType === 'application/octet-stream') {
// 如果浏览器不能识别文件类型,则进行手动检查
resolve(checkFileType());
} else {
// 如果浏览器可以识别文件类型,则根据文件类型进行检查
resolve(isValidZipFileType(fileType));
}
};
fileReader.readAsArrayBuffer(file.slice(0, 4));
});
}
Loading…
Cancel
Save