diff --git a/src/app/actions/fileUpload.ts b/src/app/actions/fileUpload.ts index c3fb85b..60917c9 100644 --- a/src/app/actions/fileUpload.ts +++ b/src/app/actions/fileUpload.ts @@ -2,6 +2,8 @@ import { API_BASE_URL } from '@/app/actions/definitions'; import { getSessionData } from '@/app/actions/session'; +import { FormState } from '@/app/actions/auth'; +import { unknown } from 'zod'; interface initMessageBody { file_name: string, @@ -101,7 +103,12 @@ export async function cancelUpload(udloadId: string) { } } -export async function chunkUpload(formData: FormData) { +export type ChunkResponse = { + errorMesage: string | null + fileId?: string +}; + +export async function chunkUpload(formData: FormData): Promise { try { const response = await fetch(`${API_BASE_URL}/api/v1/files/chunk`, { method: 'POST', @@ -111,17 +118,35 @@ export async function chunkUpload(formData: FormData) { if (response.ok) { let parsed = await response.json(); - if (parsed.message_desc === 'Upload cancelled successfully') { - return parsed.message_body; + if (parsed.message_code === 0) { + console.log('code 0'); + return { + errorMesage: null, + fileId: parsed.message_body.file_id + } + } else if (parsed.message_desc === 'Duplicate file upload') { + throw 'duplicate-file' + } else if (parsed.message_desc === 'Failed to upload chunk: Failed to upload chunk: User not have tokens for protect') { + throw 'user-not-have-tokens-for-protect' } else { - throw parsed; + throw 'error' } } else { - throw (`${response.status}`); + throw 'error' + } + } catch (error: unknown) { + if (error) { + const typedError = error as any; + + return { + errorMesage: typedError + }; + } else { + return { + errorMesage: 'error' + }; } - } catch (error) { - return error } } diff --git a/src/app/components/test-component.tsx b/src/app/components/TestComponent.tsx similarity index 100% rename from src/app/components/test-component.tsx rename to src/app/components/TestComponent.tsx diff --git a/src/app/components/UploadSectionFile.tsx b/src/app/components/UploadSectionFile.tsx index af37ccb..bbfae50 100644 --- a/src/app/components/UploadSectionFile.tsx +++ b/src/app/components/UploadSectionFile.tsx @@ -234,15 +234,11 @@ export default function UploadSectionFile({ fileType, allowedExtensions, maxFile formData.append('upload_id', response.upload_id); formData.append('chunk_number', chunkIndex.toString()); formData.append('chunk', chunk); - /* formData.append('findSimilar', '0'); */ const chunkResponse = await chunkUpload(formData); - if (chunkResponse.message_desc !== 'Chunk uploaded successfully') { - if (chunkResponse.message_desc === 'Duplicate file upload') { - throw (`duplicate-file`); - } - throw new Error(`Chunk ${chunkIndex} upload failed`); + if (chunkResponse.errorMesage !== null) { + throw chunkResponse.errorMesage; } setUploadProgress(Math.floor((chunkIndex + 1) / totalChunks * 100)); @@ -260,10 +256,16 @@ export default function UploadSectionFile({ fileType, allowedExtensions, maxFile } catch (error) { if (!isCancelledRef.current) { - if (error === 'duplicate-file') { - setErrorHandler(t('error-duplicate-file')); - } else { - setErrorHandler(t('error-uploading-file')); + switch (error) { + case 'duplicate-file': + setErrorHandler(t('error-duplicate-file')); + break; + case 'user-not-have-tokens-for-protect': + setErrorHandler(t('error-user-not-have-tokens-for-protect')); + break; + default: + setErrorHandler(t('error-uploading-file')); + break; } } } finally { diff --git a/src/app/ui/search/section-search-file.tsx b/src/app/ui/search/section-search-file.tsx index 4a7e0f3..e93ea28 100644 --- a/src/app/ui/search/section-search-file.tsx +++ b/src/app/ui/search/section-search-file.tsx @@ -196,15 +196,15 @@ export default function SectionSearchFile({ fileType, allowedExtensions, maxFile const chunkResponse = await chunkUpload(formData); - if (chunkResponse.message_desc !== 'Chunk uploaded successfully') { - throw new Error(`Chunk ${chunkIndex} upload failed`); + if (chunkResponse.errorMesage !== null) { + throw chunkResponse.errorMesage; } setUploadProgress(Math.floor((chunkIndex + 1) / totalChunks * 100)); - if (isLastChunk) { - setFileId(chunkResponse.message_body.file_id); - document.cookie = `searchedFileId=${chunkResponse.message_body.file_id}` + if (isLastChunk && chunkResponse.fileId) { + setFileId(chunkResponse.fileId); + document.cookie = `searchedFileId=${chunkResponse.fileId}` } } diff --git a/src/i18n/messages/en.json b/src/i18n/messages/en.json index 52561a9..0342d9f 100644 --- a/src/i18n/messages/en.json +++ b/src/i18n/messages/en.json @@ -205,7 +205,8 @@ "FAILED_SAVE": "Failed save", "view": "View", "are-no-violations": "There are no violations", - "error-reading-image": "Error reading image" + "error-reading-image": "Error reading image", + "error-user-not-have-tokens-for-protect": "User not have tokens for protect" }, "Login-register-form": { "and": "and", diff --git a/src/i18n/messages/ru.json b/src/i18n/messages/ru.json index fc106bd..2a74095 100644 --- a/src/i18n/messages/ru.json +++ b/src/i18n/messages/ru.json @@ -205,7 +205,8 @@ "FAILED_SAVE": "Сохранение не удалось", "view": "Посмотреть", "are-no-violations": "Нарушений нет", - "error-reading-image": "Ошибка чтения изображения" + "error-reading-image": "Ошибка чтения изображения", + "error-user-not-have-tokens-for-protect": "У пользователя нет токенов для защиты." }, "Login-register-form": { "and": "и",