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