Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

locker-web: skip empty uploads and fail preflight early

Neeraj Guptaf7ef06a6edb9
diff --git a/web/apps/locker/src/components/CreateItemDialog.tsx b/web/apps/locker/src/components/CreateItemDialog.tsx
index 684a5ad814..eb316c4a1d 100644
--- a/web/apps/locker/src/components/CreateItemDialog.tsx
+++ b/web/apps/locker/src/components/CreateItemDialog.tsx
@@ -105,6 +105,13 @@ interface CreateItemDialogProps {
onEnsureCollections?: (
names: string[],
) => Promise<Map<string, number> | Record<string, number>>;
+ onEnsureUploadLimitState?: () => Promise<
+ | {
+ isProductionEndpoint: boolean;
+ userDetails: LockerUploadLimitState;
+ }
+ | undefined
+ >;
defaultCollectionID?: number | null;
isProductionEndpoint: boolean;
initialItems?: LockerUploadCandidate[];
@@ -122,6 +129,7 @@ export const CreateItemDialog: React.FC<CreateItemDialogProps> = ({
onUploadsFinished,
onCreateCollection,
onEnsureCollections,
+ onEnsureUploadLimitState,
defaultCollectionID,
isProductionEndpoint,
initialItems,
@@ -178,6 +186,7 @@ export const CreateItemDialog: React.FC<CreateItemDialogProps> = ({
onUploadItemComplete,
onUploadsFinished,
onEnsureCollections,
+ onEnsureUploadLimitState,
defaultCollectionID,
isProductionEndpoint,
initialItems,
diff --git a/web/apps/locker/src/components/createItemDialog/fileUploadHelpers.ts b/web/apps/locker/src/components/createItemDialog/fileUploadHelpers.ts
index 9e1c8710e2..80f14ed3c4 100644
--- a/web/apps/locker/src/components/createItemDialog/fileUploadHelpers.ts
+++ b/web/apps/locker/src/components/createItemDialog/fileUploadHelpers.ts
@@ -71,6 +71,9 @@ export const collectionNamesByUploadItem = (
]),
);
+export const filterNonEmptyUploadItems = (items: LockerUploadCandidate[]) =>
+ items.filter((item) => item.file.size > 0);
+
export const uploadProgressValue = (
progress: LockerUploadProgress | null | undefined,
uploadCap: number,
diff --git a/web/apps/locker/src/components/createItemDialog/useCreateItemDialogState.ts b/web/apps/locker/src/components/createItemDialog/useCreateItemDialogState.ts
index ae85389ceb..12cad44731 100644
--- a/web/apps/locker/src/components/createItemDialog/useCreateItemDialogState.ts
+++ b/web/apps/locker/src/components/createItemDialog/useCreateItemDialogState.ts
@@ -27,6 +27,7 @@ import {
import {
collectionNamesByUploadItem,
dedupeCollectionNames,
+ filterNonEmptyUploadItems,
normalizeCollectionName,
uploadQueueItemKey,
} from "./fileUploadHelpers";
@@ -62,6 +63,13 @@ interface UseCreateItemDialogStateProps {
onEnsureCollections?: (
names: string[],
) => Promise<Map<string, number> | Record<string, number>>;
+ onEnsureUploadLimitState?: () => Promise<
+ | {
+ isProductionEndpoint: boolean;
+ userDetails: LockerUploadLimitState;
+ }
+ | undefined
+ >;
defaultCollectionID?: number | null;
isProductionEndpoint: boolean;
initialItems?: LockerUploadCandidate[];
@@ -118,6 +126,7 @@ export const useCreateItemDialogState = ({
onUploadItemComplete,
onUploadsFinished,
onEnsureCollections,
+ onEnsureUploadLimitState,
defaultCollectionID,
isProductionEndpoint,
initialItems,
@@ -173,7 +182,7 @@ export const useCreateItemDialogState = ({
const [showPassword, setShowPassword] = useState(false);
const [selectedUploadItems, setSelectedUploadItems] = useState<
LockerUploadCandidate[]
- >(initialItems ?? []);
+ >(filterNonEmptyUploadItems(initialItems ?? []));
const [
selectedCollectionNamesByFileKey,
setSelectedCollectionNamesByFileKey,
@@ -260,11 +269,12 @@ export const useCreateItemDialogState = ({
);
setFormData(editFormData(editItem));
setShowPassword(false);
- setSelectedUploadItems(initialItems ?? []);
+ const filteredInitialItems = filterNonEmptyUploadItems(initialItems ?? []);
+ setSelectedUploadItems(filteredInitialItems);
setCustomCollectionNames([]);
setSelectedCollectionNamesByFileKey(
collectionNamesByUploadItem(
- initialItems ?? [],
+ filteredInitialItems,
defaultCollectionName,
),
);
@@ -402,10 +412,14 @@ export const useCreateItemDialogState = ({
relativePath: file.webkitRelativePath || file.name,
suggestedCollectionNames: [],
}));
- setSelectedUploadItems(items);
+ const nonEmptyItems = filterNonEmptyUploadItems(items);
+ setSelectedUploadItems(nonEmptyItems);
setCustomCollectionNames([]);
setSelectedCollectionNamesByFileKey(
- collectionNamesByUploadItem(items, defaultCollectionName),
+ collectionNamesByUploadItem(
+ nonEmptyItems,
+ defaultCollectionName,
+ ),
);
resetUploadState();
setError(null);
@@ -455,25 +469,41 @@ export const useCreateItemDialogState = ({
return;
}
- const pendingUploadItems = selectedUploadItems.filter(
- (item) => !completedFileKeys.has(uploadQueueItemKey(item)),
+ const pendingUploadItems = filterNonEmptyUploadItems(
+ selectedUploadItems.filter(
+ (item) => !completedFileKeys.has(uploadQueueItemKey(item)),
+ ),
);
if (pendingUploadItems.length === 0) {
return;
}
+ let effectiveUserDetails = userDetails;
+ let effectiveIsProductionEndpoint = isProductionEndpoint;
+ if (!effectiveUserDetails) {
+ const uploadLimitState = await onEnsureUploadLimitState?.();
+ effectiveUserDetails = uploadLimitState?.userDetails;
+ effectiveIsProductionEndpoint =
+ uploadLimitState?.isProductionEndpoint ??
+ effectiveIsProductionEndpoint;
+ }
+
+ if (!effectiveUserDetails) {
+ setError(t("generic_error_retry"));
+ setUpgradeCTAType(null);
+ return;
+ }
+
const preflightFailure = validateLockerUploadBatch(
pendingUploadItems.map(({ file }) => file),
- userDetails,
- isProductionEndpoint,
+ effectiveUserDetails,
+ effectiveIsProductionEndpoint,
);
if (preflightFailure) {
const preflightFailureMessage = (
failure: LockerUploadPreflightFailure,
) => {
switch (failure.reason) {
- case "emptyFile":
- return t("uploadEmptyFileErrorBody");
case "fileCountLimit":
return t("uploadFileCountLimitErrorBody");
case "fileTooLarge":
@@ -661,6 +691,7 @@ export const useCreateItemDialogState = ({
displayCollections,
handleClose,
onEnsureCollections,
+ onEnsureUploadLimitState,
isProductionEndpoint,
onUploadItemComplete,
onUploadProgress,
diff --git a/web/apps/locker/src/components/lockerPage/useLockerActions.tsx b/web/apps/locker/src/components/lockerPage/useLockerActions.tsx
index 987f999780..54c49e9ba0 100644
--- a/web/apps/locker/src/components/lockerPage/useLockerActions.tsx
+++ b/web/apps/locker/src/components/lockerPage/useLockerActions.tsx
@@ -33,6 +33,7 @@ import type {
LockerUploadCandidate,
} from "types";
import { getItemTitle, isCollectionOwner } from "types";
+import { filterNonEmptyUploadItems } from "../createItemDialog/fileUploadHelpers";
import type { CreateItemDialogEditItem } from "../createItemDialog/useCreateItemDialogState";
type DragDataTransferItem = DataTransferItem & {
@@ -612,10 +613,11 @@ export const useLockerActions = ({
const openUploadDialogForItems = useCallback(
(items: LockerUploadCandidate[]) => {
- if (items.length === 0) {
+ const nonEmptyItems = filterNonEmptyUploadItems(items);
+ if (nonEmptyItems.length === 0) {
return;
}
- setPrefilledUploadItems(items);
+ setPrefilledUploadItems(nonEmptyItems);
setCreateDialogOpen(true);
},
[],
diff --git a/web/apps/locker/src/pages/locker.tsx b/web/apps/locker/src/pages/locker.tsx
index 9d01dc915f..f6016e1678 100644
--- a/web/apps/locker/src/pages/locker.tsx
+++ b/web/apps/locker/src/pages/locker.tsx
@@ -44,6 +44,7 @@ export const LockerPage: React.FC = () => {
const {
collections,
+ ensureUploadLimitState,
hasFetched,
initialLoadError,
isProductionEndpoint,
@@ -287,6 +288,7 @@ export const LockerPage: React.FC = () => {
onUploadsFinished={handleUploadsFinished}
onCreateCollection={handleCreateCollection}
onEnsureCollections={ensureCollectionsExist}
+ onEnsureUploadLimitState={ensureUploadLimitState}
defaultCollectionID={selectedCollectionID}
isProductionEndpoint={isProductionEndpoint}
initialItems={prefilledUploadItems}
diff --git a/web/apps/locker/src/services/locker-limits.ts b/web/apps/locker/src/services/locker-limits.ts
index 24fcbeb476..53d889721f 100644
--- a/web/apps/locker/src/services/locker-limits.ts
+++ b/web/apps/locker/src/services/locker-limits.ts
@@ -33,7 +33,6 @@ export interface LockerUploadLimitState {
}
export type LockerUploadPreflightFailureReason =
- | "emptyFile"
| "fileCountLimit"
| "fileTooLarge"
| "storageLimit";
@@ -48,11 +47,6 @@ export const validateLockerUploadBatch = (
userDetails: LockerUploadLimitState | undefined,
isProductionEndpoint: boolean,
): LockerUploadPreflightFailure | null => {
- const emptyFile = files.find((file) => file.size === 0);
- if (emptyFile) {
- return { reason: "emptyFile", fileName: emptyFile.name };
- }
-
const oversizedFile = files.find(
(file) => file.size > LOCKER_MAX_FILE_SIZE_BYTES,
);