This commit is contained in:
Estifo77
2026-07-28 14:50:53 +03:00
parent 4a02a4fa8f
commit 3edd0587e5

View File

@@ -1,6 +1,6 @@
import { useCallback, useState } from 'react';
import { baseApi } from '../base-api';
import { resolveTokenFromStorage } from '../session';
import { useCallback, useState } from "react";
import { baseApi } from "../base-api";
import { resolveTokenFromStorage } from "../session";
export interface UploadFileInfo {
bucket: string;
@@ -17,13 +17,16 @@ export interface UploadKeyResponse {
const fileUploadApi = baseApi.injectEndpoints({
endpoints: (builder) => ({
getFileUploadKey: builder.mutation<UploadKeyResponse, { endpoint: string; file: File }>({
getFileUploadKey: builder.mutation<
UploadKeyResponse,
{ endpoint: string; file: File }
>({
query: ({ endpoint, file }) => ({
url: endpoint,
method: 'POST',
method: "POST",
body: {
fileName: file.name,
contentType: file.type || 'application/octet-stream',
contentType: file.type || "application/octet-stream",
size: file.size,
originalname: file.name,
},
@@ -35,29 +38,33 @@ const fileUploadApi = baseApi.injectEndpoints({
export const { useGetFileUploadKeyMutation } = fileUploadApi;
export const STORAGE_UPLOAD_ERROR = 'STORAGE_UPLOAD_ERROR';
export const STORAGE_UPLOAD_TOO_LARGE = 'STORAGE_UPLOAD_TOO_LARGE';
export const STORAGE_UPLOAD_ERROR = "STORAGE_UPLOAD_ERROR";
export const STORAGE_UPLOAD_TOO_LARGE = "STORAGE_UPLOAD_TOO_LARGE";
export function isStorageUploadError(
error: unknown,
): error is Error & { message: typeof STORAGE_UPLOAD_ERROR | typeof STORAGE_UPLOAD_TOO_LARGE } {
export function isStorageUploadError(error: unknown): error is Error & {
message: typeof STORAGE_UPLOAD_ERROR | typeof STORAGE_UPLOAD_TOO_LARGE;
} {
return (
error instanceof Error &&
(error.message === STORAGE_UPLOAD_ERROR || error.message === STORAGE_UPLOAD_TOO_LARGE)
(error.message === STORAGE_UPLOAD_ERROR ||
error.message === STORAGE_UPLOAD_TOO_LARGE)
);
}
// Bare fetch on purpose: this PUT targets the presigned storage URL directly, not
// VITE_BASE_API_URL, so it must not go through baseApi/fetchBaseQuery.
async function uploadToPresigned(file: File, presignedUrl: string): Promise<void> {
async function uploadToPresigned(
file: File,
presignedUrl: string,
): Promise<void> {
if (!presignedUrl) throw new Error(STORAGE_UPLOAD_ERROR);
const token = resolveTokenFromStorage();
const res = await fetch(presignedUrl, {
method: 'PUT',
method: "PUT",
body: file,
headers: {
'Content-Type': file.type || 'application/octet-stream',
"Content-Type": file.type || "application/octet-stream",
...(token ? { Authorization: `Bearer ${token}` } : {}),
},
});
@@ -73,10 +80,13 @@ export function useDocumentUpload() {
const [isUploading, setIsUploading] = useState(false);
const upload = useCallback(
async (file: File, endpoint: string): Promise<UploadKeyResponse> => {
async (file: File, endpoint = "/documents/get-file-upload-key"): Promise<UploadKeyResponse> => {
setIsUploading(true);
try {
const { presigned, fileInfo } = await getFileUploadKey({ endpoint, file }).unwrap();
const { presigned, fileInfo } = await getFileUploadKey({
endpoint,
file,
}).unwrap();
await uploadToPresigned(file, presigned);
return { presigned, fileInfo };
} finally {