Skip to content

armgit / ente

Public Imported from GitHub
0

💚 End-to-end encrypted cloud for everything.

[locker] Fix bugs around validation and restore

Neeraj Gupta581454d00902
diff --git a/web/apps/locker/src/components/CreateItemDialog.tsx b/web/apps/locker/src/components/CreateItemDialog.tsx
index ad9c8be421..684a5ad814 100644
--- a/web/apps/locker/src/components/CreateItemDialog.tsx
+++ b/web/apps/locker/src/components/CreateItemDialog.tsx
@@ -32,6 +32,7 @@ import { LoadingButton } from "ente-base/components/mui/LoadingButton";
import { t } from "i18next";
import React, { useRef } from "react";
import { Trans } from "react-i18next";
+import type { LockerUploadLimitState } from "services/locker-limits";
import type { LockerUploadProgress } from "services/remote";
import type {
LockerCollection,
@@ -105,8 +106,10 @@ interface CreateItemDialogProps {
names: string[],
) => Promise<Map<string, number> | Record<string, number>>;
defaultCollectionID?: number | null;
+ isProductionEndpoint: boolean;
initialItems?: LockerUploadCandidate[];
editItem?: CreateItemDialogEditItem | null;
+ userDetails?: LockerUploadLimitState;
}
export const CreateItemDialog: React.FC<CreateItemDialogProps> = ({
@@ -120,8 +123,10 @@ export const CreateItemDialog: React.FC<CreateItemDialogProps> = ({
onCreateCollection,
onEnsureCollections,
defaultCollectionID,
+ isProductionEndpoint,
initialItems,
editItem,
+ userDetails,
}) => {
const fileInputRef = useRef<HTMLInputElement>(null);
const {
@@ -174,8 +179,10 @@ export const CreateItemDialog: React.FC<CreateItemDialogProps> = ({
onUploadsFinished,
onEnsureCollections,
defaultCollectionID,
+ isProductionEndpoint,
initialItems,
editItem,
+ userDetails,
});
return (
diff --git a/web/apps/locker/src/components/ItemList.tsx b/web/apps/locker/src/components/ItemList.tsx
index fe32fb9a3f..e918ed3e86 100644
--- a/web/apps/locker/src/components/ItemList.tsx
+++ b/web/apps/locker/src/components/ItemList.tsx
@@ -51,6 +51,7 @@ import {
hasDownloadableObject,
isImportantCollection,
isLockerItemOwner,
+ restoreTargetLockerCollections,
sortLockerCollections,
visibleLockerCollections,
} from "types";
@@ -176,6 +177,13 @@ export const ItemList: React.FC<ItemListProps> = ({
),
[collections, isCollectionsView],
);
+ const restoreCollections = useMemo(
+ () =>
+ uniqueCollectionsByID(
+ restoreTargetLockerCollections(collections, currentUserID),
+ ),
+ [collections, currentUserID],
+ );
const allItems = useMemo(() => {
const itemsByID = new Map<number, LockerItem>();
for (const collection of collections) {
@@ -408,6 +416,17 @@ export const ItemList: React.FC<ItemListProps> = ({
}
}, [restoreItem, restoreItemID]);
+ useEffect(() => {
+ if (
+ restoreCollectionID !== null &&
+ !restoreCollections.some(
+ (collection) => collection.id === restoreCollectionID,
+ )
+ ) {
+ setRestoreCollectionID(null);
+ }
+ }, [restoreCollectionID, restoreCollections]);
+
useEffect(() => {
if (restoreItemID === null) {
setRestoreError(null);
@@ -1295,7 +1314,6 @@ export const ItemList: React.FC<ItemListProps> = ({
createCollectionOpen={createCollectionOpen}
creatingCollection={creatingCollection}
deleteFileLink={() => void deleteActiveFileLink()}
- displayCollections={displayCollections}
dropdownHomeCollections={dropdownHomeCollections}
feedbackMessage={feedbackMessage}
fileLinkDialogOpen={activeFileLinkItem !== null}
@@ -1337,6 +1355,7 @@ export const ItemList: React.FC<ItemListProps> = ({
renameCollectionOpen={renameCollectionID !== null}
renamingCollection={renamingCollection}
renameValue={renameValue}
+ restoreCollections={restoreCollections}
restoreError={restoreError}
restoreCollectionID={restoreCollectionID}
restoreDialogOpen={restoreItem !== null}
diff --git a/web/apps/locker/src/components/createItemDialog/useCreateItemDialogState.ts b/web/apps/locker/src/components/createItemDialog/useCreateItemDialogState.ts
index 2423dc2f0f..ae85389ceb 100644
--- a/web/apps/locker/src/components/createItemDialog/useCreateItemDialogState.ts
+++ b/web/apps/locker/src/components/createItemDialog/useCreateItemDialogState.ts
@@ -8,6 +8,11 @@ import {
lockerUpgradeCTAType,
type LockerUpgradeCTAType,
} from "services/locker-errors";
+import {
+ validateLockerUploadBatch,
+ type LockerUploadLimitState,
+ type LockerUploadPreflightFailure,
+} from "services/locker-limits";
import type { LockerUploadProgress } from "services/remote";
import type {
LockerCollection,
@@ -58,8 +63,10 @@ interface UseCreateItemDialogStateProps {
names: string[],
) => Promise<Map<string, number> | Record<string, number>>;
defaultCollectionID?: number | null;
+ isProductionEndpoint: boolean;
initialItems?: LockerUploadCandidate[];
editItem?: CreateItemDialogEditItem | null;
+ userDetails?: LockerUploadLimitState;
}
interface UploadState {
@@ -112,8 +119,10 @@ export const useCreateItemDialogState = ({
onUploadsFinished,
onEnsureCollections,
defaultCollectionID,
+ isProductionEndpoint,
initialItems,
editItem,
+ userDetails,
}: UseCreateItemDialogStateProps) => {
const isEditMode = !!editItem;
const currentUserID = savedLocalUser()?.id;
@@ -453,6 +462,38 @@ export const useCreateItemDialogState = ({
return;
}
+ const preflightFailure = validateLockerUploadBatch(
+ pendingUploadItems.map(({ file }) => file),
+ userDetails,
+ isProductionEndpoint,
+ );
+ if (preflightFailure) {
+ const preflightFailureMessage = (
+ failure: LockerUploadPreflightFailure,
+ ) => {
+ switch (failure.reason) {
+ case "emptyFile":
+ return t("uploadEmptyFileErrorBody");
+ case "fileCountLimit":
+ return t("uploadFileCountLimitErrorBody");
+ case "fileTooLarge":
+ return t("uploadFileTooLargeErrorBody");
+ case "storageLimit":
+ return t("uploadStorageLimitErrorBody");
+ }
+ };
+
+ setError(preflightFailureMessage(preflightFailure));
+ setUpgradeCTAType(
+ preflightFailure.reason === "fileCountLimit"
+ ? "fileCountLimit"
+ : preflightFailure.reason === "storageLimit"
+ ? "storageLimit"
+ : null,
+ );
+ return;
+ }
+
setUploading(true);
setError(null);
setUpgradeCTAType(null);
@@ -620,11 +661,13 @@ export const useCreateItemDialogState = ({
displayCollections,
handleClose,
onEnsureCollections,
+ isProductionEndpoint,
onUploadItemComplete,
onUploadProgress,
onUploadsFinished,
selectedCollectionNamesByFileKey,
selectedUploadItems,
+ userDetails,
]);
const canSave =
diff --git a/web/apps/locker/src/components/itemList/ItemListDialogs.tsx b/web/apps/locker/src/components/itemList/ItemListDialogs.tsx
index c2ad70cdd0..9787092f8e 100644
--- a/web/apps/locker/src/components/itemList/ItemListDialogs.tsx
+++ b/web/apps/locker/src/components/itemList/ItemListDialogs.tsx
@@ -32,7 +32,6 @@ interface ItemListDialogsProps {
createCollectionOpen: boolean;
creatingCollection: boolean;
deleteFileLink: () => void;
- displayCollections: LockerCollection[];
dropdownHomeCollections: LockerCollection[];
feedbackMessage: string | null;
homeSelectedCollectionIDs: number[];
@@ -54,6 +53,7 @@ interface ItemListDialogsProps {
renameCollectionOpen: boolean;
renamingCollection: boolean;
renameValue: string;
+ restoreCollections: LockerCollection[];
restoreError: string | null;
restoreCollectionID: number | null;
restoreDialogOpen: boolean;
@@ -78,7 +78,6 @@ export const ItemListDialogs: React.FC<ItemListDialogsProps> = ({
createCollectionOpen,
creatingCollection,
deleteFileLink,
- displayCollections,
dropdownHomeCollections,
feedbackMessage,
homeSelectedCollectionIDs,
@@ -100,6 +99,7 @@ export const ItemListDialogs: React.FC<ItemListDialogsProps> = ({
renameCollectionOpen,
renamingCollection,
renameValue,
+ restoreCollections,
restoreError,
restoreCollectionID,
restoreDialogOpen,
@@ -180,8 +180,8 @@ export const ItemListDialogs: React.FC<ItemListDialogsProps> = ({
<DialogTitle>{t("restoreToCollection")}</DialogTitle>
<DialogContent>
<Stack sx={{ gap: 1, pt: 0.25, pb: 1 }}>
- {displayCollections.length > 0 ? (
- displayCollections.map((collection) => (
+ {restoreCollections.length > 0 ? (
+ restoreCollections.map((collection) => (
<Chip
key={collection.id}
label={collection.name}
diff --git a/web/apps/locker/src/components/lockerPage/useLockerData.ts b/web/apps/locker/src/components/lockerPage/useLockerData.ts
index bb029cbbd2..72a71f4cbd 100644
--- a/web/apps/locker/src/components/lockerPage/useLockerData.ts
+++ b/web/apps/locker/src/components/lockerPage/useLockerData.ts
@@ -16,6 +16,7 @@ import {
isEnteProductionEndpoint,
LOCKER_FILE_LIMIT_FREE,
LOCKER_FILE_LIMIT_PAID,
+ type LockerUploadLimitState,
} from "services/locker-limits";
import { fetchLockerData, fetchLockerTrash } from "services/remote";
import type { LockerCollection, LockerItem } from "types";
@@ -38,14 +39,8 @@ interface LockerUserDetailsResponse {
};
}
-export interface UserDetails {
+export interface UserDetails extends LockerUploadLimitState {
email: string;
- usage: number;
- storageLimit: number;
- fileCount: number;
- lockerFileLimit: number;
- isPartOfFamily: boolean;
- lockerFamilyFileCount?: number;
}
const hasPaidLockerAccess = (json: {
diff --git a/web/apps/locker/src/locales/en-US/translation.json b/web/apps/locker/src/locales/en-US/translation.json
index c31089a6b1..a1106453fa 100644
--- a/web/apps/locker/src/locales/en-US/translation.json
+++ b/web/apps/locker/src/locales/en-US/translation.json
@@ -66,6 +66,7 @@
"uploadFileCountLimitErrorBody": "You've reached your file limit. Upgrade to a paid plan for more storage.",
"uploadFileCountLimitErrorBodyWithUpgrade": "You've reached your file limit. Please <cta>upgrade</cta> to a paid plan.",
"uploadFileCountLimitErrorToast": "Action failed. You've reached your file limit.",
+ "uploadEmptyFileErrorBody": "Empty files can't be uploaded.",
"uploadFileTooLargeErrorBody": "This file is larger than the maximum supported upload size of 10GB.",
"download": "Download",
"downloading": "Downloading...",
diff --git a/web/apps/locker/src/locales/fr-FR/translation.json b/web/apps/locker/src/locales/fr-FR/translation.json
index d62a6c95f4..f3685347b3 100644
--- a/web/apps/locker/src/locales/fr-FR/translation.json
+++ b/web/apps/locker/src/locales/fr-FR/translation.json
@@ -51,6 +51,7 @@
"uploadSubscriptionExpiredErrorBody": "Ton abonnement Ente Photos a expire. Renouvelle ton abonnement pour continuer a televerser.",
"uploadFileCountLimitErrorBody": "Tu as atteint ta limite de fichiers. Passe a un forfait payant pour plus de stockage.",
"uploadFileCountLimitErrorToast": "Action echouee. Tu as atteint ta limite de fichiers.",
+ "uploadEmptyFileErrorBody": "Les fichiers vides ne peuvent pas etre televerses.",
"uploadFileTooLargeErrorBody": "Ce fichier depasse la taille maximale de telechargement prise en charge de 10 Go.",
"downloading": "Téléchargement en cours...",
"downloadFailed": "Échec du téléchargement",
diff --git a/web/apps/locker/src/locales/ja-JP/translation.json b/web/apps/locker/src/locales/ja-JP/translation.json
index b8b636896c..2c6cd05795 100644
--- a/web/apps/locker/src/locales/ja-JP/translation.json
+++ b/web/apps/locker/src/locales/ja-JP/translation.json
@@ -55,6 +55,7 @@
"uploadSubscriptionExpiredErrorBody": "Ente Photos のサブスクリプションの有効期限が切れています。アップロードを続けるには更新してください。",
"uploadFileCountLimitErrorBody": "ファイル制限に達しました。有料プランにアップグレードして、より多くのストレージを利用してください。",
"uploadFileCountLimitErrorToast": "操作に失敗しました。ファイル制限に達しています。",
+ "uploadEmptyFileErrorBody": "空のファイルはアップロードできません。",
"uploadFileTooLargeErrorBody": "このファイルはサポートされている最大アップロードサイズ10GBを超えています。",
"downloading": "ダウンロード中…",
"downloadFailed": "ダウンロード失敗",
diff --git a/web/apps/locker/src/pages/locker.tsx b/web/apps/locker/src/pages/locker.tsx
index 4bd25fa396..9d01dc915f 100644
--- a/web/apps/locker/src/pages/locker.tsx
+++ b/web/apps/locker/src/pages/locker.tsx
@@ -288,7 +288,9 @@ export const LockerPage: React.FC = () => {
onCreateCollection={handleCreateCollection}
onEnsureCollections={ensureCollectionsExist}
defaultCollectionID={selectedCollectionID}
+ isProductionEndpoint={isProductionEndpoint}
initialItems={prefilledUploadItems}
+ userDetails={userDetails}
/>
{editItem && (
@@ -299,6 +301,8 @@ export const LockerPage: React.FC = () => {
onSave={handleUpdateItem}
onCreateCollection={handleCreateCollection}
editItem={editItem}
+ isProductionEndpoint={isProductionEndpoint}
+ userDetails={userDetails}
/>
)}
diff --git a/web/apps/locker/src/services/locker-limits.ts b/web/apps/locker/src/services/locker-limits.ts
index c5dae0c56c..24fcbeb476 100644
--- a/web/apps/locker/src/services/locker-limits.ts
+++ b/web/apps/locker/src/services/locker-limits.ts
@@ -2,6 +2,8 @@ import { apiOrigin } from "ente-base/origins";
export const LOCKER_FILE_LIMIT_FREE = 100;
export const LOCKER_FILE_LIMIT_PAID = 1000;
+export const LOCKER_MAX_FILE_SIZE_BYTES = 10 * 1024 * 1024 * 1024;
+export const LOCKER_STORAGE_BUFFER_BYTES = 20 * 1024 * 1024;
const ENTE_PRODUCTION_API_ORIGIN = "https://api.ente.io";
@@ -20,3 +22,68 @@ export const effectiveLockerFileLimit = (
? LOCKER_FILE_LIMIT_PAID
: normalizedLimit;
};
+
+export interface LockerUploadLimitState {
+ usage: number;
+ storageLimit: number;
+ fileCount: number;
+ lockerFileLimit: number;
+ isPartOfFamily: boolean;
+ lockerFamilyFileCount?: number;
+}
+
+export type LockerUploadPreflightFailureReason =
+ | "emptyFile"
+ | "fileCountLimit"
+ | "fileTooLarge"
+ | "storageLimit";
+
+export interface LockerUploadPreflightFailure {
+ reason: LockerUploadPreflightFailureReason;
+ fileName?: string;
+}
+
+export const validateLockerUploadBatch = (
+ files: File[],
+ 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,
+ );
+ if (oversizedFile) {
+ return { reason: "fileTooLarge", fileName: oversizedFile.name };
+ }
+
+ if (!userDetails) {
+ return null;
+ }
+
+ const maxFileCount = effectiveLockerFileLimit(
+ userDetails.lockerFileLimit,
+ isProductionEndpoint,
+ );
+ const currentFileCount =
+ userDetails.isPartOfFamily &&
+ typeof userDetails.lockerFamilyFileCount === "number"
+ ? userDetails.lockerFamilyFileCount
+ : userDetails.fileCount;
+ if (currentFileCount + files.length > maxFileCount) {
+ return { reason: "fileCountLimit" };
+ }
+
+ const freeStorage =
+ Math.max(userDetails.storageLimit - userDetails.usage, 0) +
+ LOCKER_STORAGE_BUFFER_BYTES;
+ const totalUploadSize = files.reduce((total, file) => total + file.size, 0);
+ if (totalUploadSize > freeStorage) {
+ return { reason: "storageLimit" };
+ }
+
+ return null;
+};
diff --git a/web/apps/locker/src/types.ts b/web/apps/locker/src/types.ts
index 95d65f9c02..899df2f4b7 100644
--- a/web/apps/locker/src/types.ts
+++ b/web/apps/locker/src/types.ts
@@ -147,6 +147,13 @@ export const isCollectionOwner = (
currentUserID: number | undefined,
) => currentUserID !== undefined && collection.owner.id === currentUserID;
+export const canRestoreToCollection = (
+ collection: LockerCollection,
+ currentUserID: number | undefined,
+) =>
+ isCollectionOwner(collection, currentUserID) &&
+ !isUncategorizedCollection(collection);
+
export const canEditCollection = (
collection: LockerCollection,
currentUserID: number | undefined,
@@ -199,6 +206,14 @@ export const sortLockerCollections = (collections: LockerCollection[]) =>
export const visibleLockerCollections = (collections: LockerCollection[]) =>
sortLockerCollections(collections);
+export const restoreTargetLockerCollections = (
+ collections: LockerCollection[],
+ currentUserID: number | undefined,
+) =>
+ visibleLockerCollections(collections).filter((collection) =>
+ canRestoreToCollection(collection, currentUserID),
+ );
+
/**
* Get the display title for a locker item.
*/