fix: normalize upload types
This commit is contained in:
@@ -2,18 +2,35 @@ import { request } from "../utils/request";
|
||||
import { getTenantCode } from "../utils/tenant";
|
||||
|
||||
export const commonApi = {
|
||||
normalizeUploadType: (type, file) => {
|
||||
const normalized = (type || "").toLowerCase();
|
||||
const mime = (file && file.type) || "";
|
||||
|
||||
if (
|
||||
normalized === "video" ||
|
||||
normalized === "audio" ||
|
||||
normalized === "image"
|
||||
) {
|
||||
return normalized;
|
||||
}
|
||||
if (mime.startsWith("video/")) return "video";
|
||||
if (mime.startsWith("audio/")) return "audio";
|
||||
return "image";
|
||||
},
|
||||
getOptions: () => request("/common/options"),
|
||||
checkHash: (hash) => request(`/upload/check?hash=${hash}`),
|
||||
deleteMedia: (id) => request(`/media-assets/${id}`, { method: "DELETE" }),
|
||||
upload: (file, type) => {
|
||||
const normalizedType = commonApi.normalizeUploadType(type, file);
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
formData.append("type", type);
|
||||
formData.append("type", normalizedType);
|
||||
return request("/upload", { method: "POST", body: formData });
|
||||
},
|
||||
uploadMultipart: (file, hash, type, onProgress) => {
|
||||
const controller = new AbortController();
|
||||
const signal = controller.signal;
|
||||
const normalizedType = commonApi.normalizeUploadType(type, file);
|
||||
|
||||
const promise = (async () => {
|
||||
// 1. Check Hash
|
||||
@@ -37,7 +54,7 @@ export const commonApi = {
|
||||
size: file.size,
|
||||
mime_type: file.type,
|
||||
hash: hash,
|
||||
type: type,
|
||||
type: normalizedType,
|
||||
},
|
||||
signal,
|
||||
});
|
||||
@@ -82,11 +99,12 @@ export const commonApi = {
|
||||
return { promise, abort: () => controller.abort() };
|
||||
},
|
||||
uploadWithProgress: (file, type, onProgress) => {
|
||||
const normalizedType = commonApi.normalizeUploadType(type, file);
|
||||
let xhr;
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
formData.append("type", type);
|
||||
formData.append("type", normalizedType);
|
||||
|
||||
xhr = new XMLHttpRequest();
|
||||
const tenantCode = getTenantCode();
|
||||
|
||||
Reference in New Issue
Block a user