mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-25 00:36:31 +02:00
Merge pull request #1298 from tmchow/osc/1245-dedupe-anon-upload
refactor(anon-chat): dedupe upload via anonymousChatApiService
This commit is contained in:
commit
f3c4daa592
3 changed files with 20 additions and 36 deletions
|
|
@ -9,7 +9,7 @@ import { Switch } from "@/components/ui/switch";
|
|||
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
||||
import { useAnonymousMode } from "@/contexts/anonymous-mode";
|
||||
import { useLoginGate } from "@/contexts/login-gate";
|
||||
import { BACKEND_URL } from "@/lib/env-config";
|
||||
import { anonymousChatApiService } from "@/lib/apis/anonymous-chat-api.service";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
const ANON_ALLOWED_EXTENSIONS = new Set([
|
||||
|
|
@ -128,24 +128,12 @@ export const FreeComposer: FC = () => {
|
|||
}
|
||||
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
const res = await fetch(`${BACKEND_URL}/api/v1/public/anon-chat/upload`, {
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (res.status === 409) {
|
||||
gate("upload more documents");
|
||||
const result = await anonymousChatApiService.uploadDocument(file);
|
||||
if (!result.ok) {
|
||||
if (result.reason === "quota_exceeded") gate("upload more documents");
|
||||
return;
|
||||
}
|
||||
if (!res.ok) {
|
||||
const body = await res.json().catch(() => ({}));
|
||||
throw new Error(body.detail || `Upload failed: ${res.status}`);
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
const data = result.data;
|
||||
if (anonMode.isAnonymous) {
|
||||
anonMode.setUploadedDoc({
|
||||
filename: data.filename,
|
||||
|
|
|
|||
|
|
@ -68,11 +68,11 @@ import type { DocumentTypeEnum } from "@/contracts/types/document.types";
|
|||
import { useDebouncedValue } from "@/hooks/use-debounced-value";
|
||||
import { useMediaQuery } from "@/hooks/use-media-query";
|
||||
import { useElectronAPI } from "@/hooks/use-platform";
|
||||
import { anonymousChatApiService } from "@/lib/apis/anonymous-chat-api.service";
|
||||
import { documentsApiService } from "@/lib/apis/documents-api.service";
|
||||
import { foldersApiService } from "@/lib/apis/folders-api.service";
|
||||
import { searchSpacesApiService } from "@/lib/apis/search-spaces-api.service";
|
||||
import { authenticatedFetch } from "@/lib/auth-utils";
|
||||
import { BACKEND_URL } from "@/lib/env-config";
|
||||
import { uploadFolderScan } from "@/lib/folder-sync-upload";
|
||||
import { getSupportedExtensionsSet } from "@/lib/supported-extensions";
|
||||
import { queries } from "@/zero/queries/index";
|
||||
|
|
@ -1312,24 +1312,12 @@ function AnonymousDocumentsSidebar({
|
|||
|
||||
setIsUploading(true);
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
const res = await fetch(`${BACKEND_URL}/api/v1/public/anon-chat/upload`, {
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (res.status === 409) {
|
||||
gate("upload more documents");
|
||||
const result = await anonymousChatApiService.uploadDocument(file);
|
||||
if (!result.ok) {
|
||||
if (result.reason === "quota_exceeded") gate("upload more documents");
|
||||
return;
|
||||
}
|
||||
if (!res.ok) {
|
||||
const body = await res.json().catch(() => ({}));
|
||||
throw new Error(body.detail || `Upload failed: ${res.status}`);
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
const data = result.data;
|
||||
if (anonMode.isAnonymous) {
|
||||
anonMode.setUploadedDoc({
|
||||
filename: data.filename,
|
||||
|
|
|
|||
|
|
@ -12,6 +12,10 @@ import { ValidationError } from "../error";
|
|||
|
||||
const BASE = "/api/v1/public/anon-chat";
|
||||
|
||||
export type AnonUploadResult =
|
||||
| { ok: true; data: { filename: string; size_bytes: number } }
|
||||
| { ok: false; reason: "quota_exceeded" };
|
||||
|
||||
class AnonymousChatApiService {
|
||||
private baseUrl: string;
|
||||
|
||||
|
|
@ -71,7 +75,7 @@ class AnonymousChatApiService {
|
|||
});
|
||||
};
|
||||
|
||||
uploadDocument = async (file: File): Promise<{ filename: string; size_bytes: number }> => {
|
||||
uploadDocument = async (file: File): Promise<AnonUploadResult> => {
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
const res = await fetch(this.fullUrl("/upload"), {
|
||||
|
|
@ -79,11 +83,15 @@ class AnonymousChatApiService {
|
|||
credentials: "include",
|
||||
body: formData,
|
||||
});
|
||||
if (res.status === 409) {
|
||||
return { ok: false, reason: "quota_exceeded" };
|
||||
}
|
||||
if (!res.ok) {
|
||||
const body = await res.json().catch(() => ({}));
|
||||
throw new Error(body.detail || `Upload failed: ${res.status}`);
|
||||
}
|
||||
return res.json();
|
||||
const data = await res.json();
|
||||
return { ok: true, data };
|
||||
};
|
||||
|
||||
getDocument = async (): Promise<{ filename: string; size_bytes: number } | null> => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue