diff --git a/src/app/components/image-viewer/ImageViewer.test.tsx b/src/app/components/image-viewer/ImageViewer.test.tsx index 96074363f..2fb5cd8c3 100644 --- a/src/app/components/image-viewer/ImageViewer.test.tsx +++ b/src/app/components/image-viewer/ImageViewer.test.tsx @@ -193,6 +193,21 @@ describe('ImageViewer', () => { expect(screen.getByText('Save to Gallery')).toBeInTheDocument(); }); + it("saves the Matrix source behind Android's sable-media URL to the gallery", async () => { + mockPlatform('android'); + const source = 'https://matrix.example.org/_matrix/client/v1/media/download/example.org/kitten'; + const src = `https://sable-media.localhost/${encodeURIComponent(source)}?__sable_media_cache=3`; + saveMediaToGallery.mockClear(); + + renderViewer({ src, info: { mimetype: 'image/png' } }); + fireEvent.contextMenu(screen.getByAltText('kitten.png')); + fireEvent.click(screen.getByText('Save to Gallery')); + + await waitFor(() => + expect(saveMediaToGallery).toHaveBeenCalledWith(source, 'kitten.png', 'image/png') + ); + }); + it('labels the primary action Save to Photos on iOS without duplicating it in the overflow menu', () => { mockPlatform('ios'); diff --git a/src/app/components/image-viewer/ImageViewer.tsx b/src/app/components/image-viewer/ImageViewer.tsx index 700b1e868..d6eacb0f3 100644 --- a/src/app/components/image-viewer/ImageViewer.tsx +++ b/src/app/components/image-viewer/ImageViewer.tsx @@ -234,7 +234,11 @@ export const ImageViewer = as<'div', ImageViewerProps>( const nextActivation = useMobileTapActivation(isMobile, () => onNext?.()); const galleryActivation = useMobileTapActivation(isMobile, () => { closeMenu(); - void saveMediaToGallery(src, downloadFilename, galleryMimeType!); + void saveMediaToGallery( + getTauriMediaSourceUrl(src) ?? src, + downloadFilename, + galleryMimeType! + ); }); const resetZoomMenuActivation = useMobileTapActivation(isMobile, () => { resetTransforms(); diff --git a/src/app/utils/download.test.ts b/src/app/utils/download.test.ts index 79848ca9b..a8e8f4cde 100644 --- a/src/app/utils/download.test.ts +++ b/src/app/utils/download.test.ts @@ -169,6 +169,42 @@ describe('saveMediaToGallery', () => { expect(showToast).toHaveBeenCalledWith('Saved to Gallery'); }); + it('writes all fetched Android image bytes before publishing the gallery file', async () => { + const bytes = new Uint8Array([0x89, 0x50, 0x4e, 0x47]); + mocks.fetch.mockResolvedValueOnce(new Response(bytes, { status: 200 })); + + await saveMediaToGallery('https://matrix.example.org/photo', 'photo.png', 'image/png'); + + expect(androidFs.writeFile).toHaveBeenCalledWith('content://media/image', bytes); + expect(androidFs.writeFile.mock.invocationCallOrder[0]).toBeLessThan( + androidFs.setPublicFilePending.mock.invocationCallOrder[0]! + ); + expect(androidFs.setPublicFilePending).toHaveBeenCalledWith('content://media/image', false); + }); + + it('does not create a gallery file when Android storage permission is denied', async () => { + androidFs.checkPublicFilesPermission.mockResolvedValue(false); + androidFs.requestPublicFilesPermission.mockResolvedValue(false); + + await saveMediaToGallery(new Blob(['data']), 'photo.png', 'image/png'); + + expect(androidFs.createNewPublicImageFile).not.toHaveBeenCalled(); + expect(androidFs.writeFile).not.toHaveBeenCalled(); + expect(showToast).toHaveBeenCalledWith( + 'Failed to save to gallery: Storage permission was denied' + ); + }); + + it('does not clean up when Android fails before creating a gallery file', async () => { + androidFs.createNewPublicImageFile.mockRejectedValue(new Error('create failed')); + + await saveMediaToGallery(new Blob(['data']), 'photo.png', 'image/png'); + + expect(androidFs.writeFile).not.toHaveBeenCalled(); + expect(androidFs.removeFile).not.toHaveBeenCalled(); + expect(showToast).toHaveBeenCalledWith('Failed to save to gallery: create failed'); + }); + it('rejects video media explicitly without touching any backend or falling back', async () => { await expect(saveMediaToGallery(new Blob(['data']), 'clip.mp4', 'video/mp4')).rejects.toThrow( 'Only image media can be saved to the gallery' @@ -191,6 +227,18 @@ describe('saveMediaToGallery', () => { expect(FileSaver.saveAs).not.toHaveBeenCalled(); }); + it.each([ + ['publishing', 'setPublicFilePending', new Error('publish failed')], + ['scanning', 'scanPublicFile', new Error('scan failed')], + ] as const)('cleans up the Android gallery file when %s fails', async (_, method, error) => { + androidFs[method].mockRejectedValue(error); + + await saveMediaToGallery(new Blob(['data']), 'photo.png', 'image/png'); + + expect(androidFs.removeFile).toHaveBeenCalledWith('content://media/image'); + expect(showToast).toHaveBeenCalledWith(`Failed to save to gallery: ${error.message}`); + }); + it('sends media bytes to the native Photos command on iOS', async () => { vi.mocked(osType).mockReturnValue('ios');