Skip to content

armgit / ente

Public Imported from GitHub
0

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

Block web upload when canvas readback is unreliable

Anand Baburajan37415165b0f7
diff --git a/web/apps/photos/src/components/CanvasReadbackBlockedDialog.tsx b/web/apps/photos/src/components/CanvasReadbackBlockedDialog.tsx
new file mode 100644
index 0000000000..12d73dbe13
--- /dev/null
+++ b/web/apps/photos/src/components/CanvasReadbackBlockedDialog.tsx
@@ -0,0 +1,161 @@
+import CloseIcon from "@mui/icons-material/Close";
+import ErrorOutlineRoundedIcon from "@mui/icons-material/ErrorOutlineRounded";
+import {
+ Box,
+ Dialog,
+ IconButton,
+ Link,
+ styled,
+ Typography,
+ type DialogProps,
+} from "@mui/material";
+import { type ModalVisibilityProps } from "ente-base/components/utils/modal";
+import { t } from "i18next";
+import React from "react";
+
+export type CanvasReadbackBlockedDialogProps = ModalVisibilityProps;
+
+export const CanvasReadbackBlockedDialog: React.FC<
+ CanvasReadbackBlockedDialogProps
+> = ({ open, onClose }) => (
+ <StyledDialog open={open} onClose={onClose}>
+ <DialogWrapper>
+ <CloseButton onClick={onClose}>
+ <CloseIcon sx={{ fontSize: 20 }} />
+ </CloseButton>
+ <ContentContainer>
+ <IllustrationWrapper>
+ <ErrorIconContainer>
+ <ErrorOutlineRoundedIcon sx={{ fontSize: 48 }} />
+ </ErrorIconContainer>
+ </IllustrationWrapper>
+
+ <TitleSection>
+ <Title>{t("thumbnail_generation_failed")}</Title>
+ <Subtitle>
+ {t("canvas_blocked_upload_description")}{" "}
+ <HelpLink
+ href="https://ente.io/help/photos/faq/troubleshooting#thumbnails"
+ target="_blank"
+ rel="noopener noreferrer"
+ >
+ {t("learn_more")}
+ </HelpLink>
+ .
+ </Subtitle>
+ </TitleSection>
+ </ContentContainer>
+ </DialogWrapper>
+ </StyledDialog>
+);
+
+const StyledDialog = styled(Dialog)<DialogProps>(({ theme }) => ({
+ "& .MuiDialog-paper": {
+ width: 381,
+ maxWidth: "calc(100% - 32px)",
+ borderRadius: 28,
+ backgroundColor: "#fff",
+ padding: 0,
+ margin: 16,
+ overflow: "visible",
+ boxShadow: "none",
+ border: "1px solid #E0E0E0",
+ ...theme.applyStyles("dark", {
+ backgroundColor: "#1b1b1b",
+ border: "1px solid rgba(255, 255, 255, 0.18)",
+ }),
+ },
+ "& .MuiBackdrop-root": { backgroundColor: "rgba(0, 0, 0, 0.5)" },
+}));
+
+const DialogWrapper = styled(Box)(() => ({
+ position: "relative",
+ padding: "48px 16px 16px 16px",
+}));
+
+const CloseButton = styled(IconButton)(({ theme }) => ({
+ position: "absolute",
+ top: 11,
+ right: 12,
+ backgroundColor: "#FAFAFA",
+ color: "#000",
+ padding: 10,
+ "&:hover": { backgroundColor: "#F0F0F0" },
+ ...theme.applyStyles("dark", {
+ backgroundColor: "rgba(255, 255, 255, 0.12)",
+ color: "#fff",
+ "&:hover": { backgroundColor: "rgba(255, 255, 255, 0.16)" },
+ }),
+}));
+
+const ContentContainer = styled(Box)(() => ({
+ display: "flex",
+ flexDirection: "column",
+ alignItems: "center",
+ gap: 16,
+}));
+
+const IllustrationWrapper = styled(Box)(() => ({
+ display: "flex",
+ justifyContent: "center",
+ alignItems: "center",
+ marginBottom: 8,
+}));
+
+const ErrorIconContainer = styled(Box)(({ theme }) => ({
+ width: 112,
+ height: 112,
+ borderRadius: 56,
+ display: "flex",
+ alignItems: "center",
+ justifyContent: "center",
+ backgroundColor: "rgba(229, 57, 53, 0.12)",
+ color: "#E53935",
+ ...theme.applyStyles("dark", {
+ backgroundColor: "rgba(255, 138, 128, 0.15)",
+ color: "#FF8A80",
+ }),
+}));
+
+const TitleSection = styled(Box)(() => ({
+ display: "flex",
+ flexDirection: "column",
+ alignItems: "center",
+ gap: 9,
+ width: "100%",
+ textAlign: "center",
+ marginBottom: 16,
+}));
+
+const Title = styled(Typography)(({ theme }) => ({
+ fontWeight: 600,
+ fontSize: 24,
+ lineHeight: "28px",
+ letterSpacing: "-0.48px",
+ color: "#000",
+ ...theme.applyStyles("dark", { color: "#fff" }),
+}));
+
+const Subtitle = styled(Typography)(({ theme }) => ({
+ fontWeight: 500,
+ fontSize: 14,
+ lineHeight: "20px",
+ color: "#666666",
+ width: "100%",
+ padding: "0 8px",
+ ...theme.applyStyles("dark", { color: "rgba(255, 255, 255, 0.7)" }),
+}));
+
+const HelpLink = styled(Link)(({ theme }) => ({
+ fontWeight: "inherit",
+ fontSize: 14,
+ lineHeight: "20px",
+ color: "#08C225",
+ textDecorationColor: "#08C225",
+ "&:hover": { color: "#07A820", textDecorationColor: "#07A820" },
+ ...theme.applyStyles("dark", {
+ color: "#22D840",
+ textDecorationColor: "#22D840",
+ "&:hover": { color: "#4BE066", textDecorationColor: "#4BE066" },
+ }),
+}));
diff --git a/web/apps/photos/src/components/Upload.tsx b/web/apps/photos/src/components/Upload.tsx
index def1d62253..e23395aa00 100644
--- a/web/apps/photos/src/components/Upload.tsx
+++ b/web/apps/photos/src/components/Upload.tsx
@@ -86,6 +86,8 @@ import type {
} from "services/upload-manager";
import { uploadManager } from "services/upload-manager";
import watcher from "services/watch";
+import { hasReliableCanvasReadback } from "utils/upload/canvas-integrity";
+import { CanvasReadbackBlockedDialog } from "./CanvasReadbackBlockedDialog";
import { UploadProgress } from "./UploadProgress";
interface UploadProps {
@@ -195,6 +197,10 @@ export const Upload: React.FC<UploadProps> = ({
const { showNotification, watchFolderView } = usePhotosAppContext();
const [uploadProgressView, setUploadProgressView] = useState(false);
+ const [
+ showCanvasReadbackBlockedDialog,
+ setShowCanvasReadbackBlockedDialog,
+ ] = useState(false);
const [uploadPhase, setUploadPhase] = useState<UploadPhase>("preparing");
const [uploadFileNames, setUploadFileNames] = useState<UploadFileNames>();
const [uploadCounter, setUploadCounter] = useState<UploadCounter>({
@@ -506,6 +512,14 @@ export const Upload: React.FC<UploadProps> = ({
}
}
+ if (!electron && !hasReliableCanvasReadback()) {
+ log.warn("Canvas readback integrity check failed; blocking upload");
+ setWebFiles([]);
+ selectedUploadType.current = undefined;
+ setShowCanvasReadbackBlockedDialog(true);
+ return;
+ }
+
uploadRunning.current = true;
props.closeUploadTypeSelector();
props.setLoading(true);
@@ -937,6 +951,10 @@ export const Upload: React.FC<UploadProps> = ({
finishedUploads={finishedUploads}
cancelUploads={cancelUploads}
/>
+ <CanvasReadbackBlockedDialog
+ open={showCanvasReadbackBlockedDialog}
+ onClose={() => setShowCanvasReadbackBlockedDialog(false)}
+ />
<SingleInputDialog
{...newAlbumNameInputVisibilityProps}
title={t("new_album")}
diff --git a/web/apps/photos/src/utils/upload/canvas-integrity.ts b/web/apps/photos/src/utils/upload/canvas-integrity.ts
new file mode 100644
index 0000000000..62f1bc23bb
--- /dev/null
+++ b/web/apps/photos/src/utils/upload/canvas-integrity.ts
@@ -0,0 +1,54 @@
+const probeWidth = 2;
+const probeHeight = 2;
+
+const expectedProbeData = Uint8ClampedArray.from([
+ 17, 83, 149, 255, 203, 29, 101, 255, 67, 131, 197, 255, 239, 53, 11, 255,
+]);
+
+const hasSamePixels = (
+ lhs: Uint8ClampedArray,
+ rhs: Uint8ClampedArray,
+): boolean => {
+ if (lhs.length !== rhs.length) return false;
+ for (let i = 0; i < lhs.length; i++) {
+ if (lhs[i] !== rhs[i]) return false;
+ }
+ return true;
+};
+
+export const hasReliableCanvasReadback = (): boolean => {
+ try {
+ const canvas = document.createElement("canvas");
+ canvas.width = probeWidth;
+ canvas.height = probeHeight;
+
+ const ctx = canvas.getContext("2d", { willReadFrequently: true });
+ if (!ctx) return false;
+
+ const probeImageData = new ImageData(
+ new Uint8ClampedArray(expectedProbeData),
+ probeWidth,
+ probeHeight,
+ );
+ ctx.putImageData(probeImageData, 0, 0);
+
+ const firstReadback = ctx.getImageData(
+ 0,
+ 0,
+ probeWidth,
+ probeHeight,
+ ).data;
+ if (!hasSamePixels(firstReadback, expectedProbeData)) return false;
+
+ const secondReadback = ctx.getImageData(
+ 0,
+ 0,
+ probeWidth,
+ probeHeight,
+ ).data;
+
+ return hasSamePixels(firstReadback, secondReadback);
+ } catch {
+ return false;
+ }
+};
diff --git a/web/packages/base/locales/en-US/translation.json b/web/packages/base/locales/en-US/translation.json
index c316266aaa..56edb5e36e 100644
--- a/web/packages/base/locales/en-US/translation.json
+++ b/web/packages/base/locales/en-US/translation.json
@@ -401,7 +401,9 @@
"failed_uploads_hint": "There will be an option to retry these when the upload finishes",
"retry_failed_uploads": "Retry failed uploads",
"thumbnail_generation_failed": "Thumbnail generation failed",
+ "canvas_blocked_upload_description": "Browser settings are blocking canvas access. Fix this before uploading so thumbnails are generated correctly.",
"thumbnail_generation_failed_hint": "These files were uploaded, but unfortunately we could not generate the thumbnails for them.",
+ "learn_more": "Learn more",
"unsupported_files": "Unsupported files",
"unsupported_files_hint": "Ente does not support these file formats yet",
"zero_size_files": "Empty files",