|
|
|
@ -5,7 +5,7 @@ import { message } from "antd"; |
|
|
|
|
|
|
|
import { useI18n } from "@/i18n"; |
|
|
|
import { authFetch } from "@/utils/authFetch"; |
|
|
|
import { uploadFileToGatewayMedia } from "@/utils/gatewayMediaUpload"; |
|
|
|
import { uploadFileToGatewayMedia, type GatewayMediaKind } from "@/utils/gatewayMediaUpload"; |
|
|
|
import { type UserFileItem, type UserFileMutationResponse } from "@/utils/userFileLibrary"; |
|
|
|
import { AssetLibraryFolderPickerModal } from "./AssetLibraryFolderPickerModal"; |
|
|
|
|
|
|
|
@ -19,6 +19,14 @@ interface AssetLibraryUploadModalProps { |
|
|
|
onClose: () => void; |
|
|
|
onUploaded: (folderId: number) => void | Promise<void>; |
|
|
|
onError?: (message: string) => void; |
|
|
|
onUploadingChange?: (uploading: boolean) => void; |
|
|
|
} |
|
|
|
|
|
|
|
function getUploadMediaKind(file: File): GatewayMediaKind | null { |
|
|
|
if (file.type.startsWith("image/")) return "image"; |
|
|
|
if (file.type.startsWith("video/")) return "video"; |
|
|
|
if (file.type.startsWith("audio/")) return "audio"; |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
export function AssetLibraryUploadModal({ |
|
|
|
@ -31,6 +39,7 @@ export function AssetLibraryUploadModal({ |
|
|
|
onClose, |
|
|
|
onUploaded, |
|
|
|
onError, |
|
|
|
onUploadingChange, |
|
|
|
}: AssetLibraryUploadModalProps) { |
|
|
|
const { t } = useI18n(); |
|
|
|
const [selectedFolderId, setSelectedFolderId] = useState<number | null>(null); |
|
|
|
@ -45,6 +54,10 @@ export function AssetLibraryUploadModal({ |
|
|
|
setSelectedFolderId((current) => current ?? folders[0]?.id ?? null); |
|
|
|
}, [folders, open]); |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
onUploadingChange?.(uploading); |
|
|
|
}, [onUploadingChange, uploading]); |
|
|
|
|
|
|
|
const handleCancel = useCallback(() => { |
|
|
|
if (uploading) return; |
|
|
|
onClose(); |
|
|
|
@ -61,14 +74,15 @@ export function AssetLibraryUploadModal({ |
|
|
|
event.target.value = ""; |
|
|
|
const targetFolderId = uploadTargetFolderIdRef.current ?? selectedId; |
|
|
|
if (!file || targetFolderId === null) return; |
|
|
|
if (!file.type.startsWith("image/")) { |
|
|
|
void message.error(t("assetLibrary.selectImageFile")); |
|
|
|
const mediaKind = getUploadMediaKind(file); |
|
|
|
if (!mediaKind) { |
|
|
|
void message.error(t("assetLibrary.selectMediaFile")); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
setUploading(true); |
|
|
|
try { |
|
|
|
const uploaded = await uploadFileToGatewayMedia(file, "image"); |
|
|
|
const uploaded = await uploadFileToGatewayMedia(file, mediaKind); |
|
|
|
const response = await authFetch("/api/popi/user-file/upload", { |
|
|
|
method: "POST", |
|
|
|
headers: { "Content-Type": "application/json" }, |
|
|
|
@ -100,7 +114,7 @@ export function AssetLibraryUploadModal({ |
|
|
|
<input |
|
|
|
ref={inputRef} |
|
|
|
type="file" |
|
|
|
accept="image/*" |
|
|
|
accept="image/*,video/*,audio/*" |
|
|
|
className="hidden" |
|
|
|
onChange={handleFileChange} |
|
|
|
/> |
|
|
|
|