Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for heic images #1618

Merged
merged 33 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
9a2babd
add support for heic
FSM1 Oct 6, 2021
4fc3b36
Update packages/files-ui/src/Components/Modules/FileBrowsers/CSFFileB…
FSM1 Oct 7, 2021
bf53439
fix lint, useMemo
FSM1 Oct 7, 2021
1ac0a7e
use thenable instead of async/await
FSM1 Oct 7, 2021
b9cef2c
fix preview tests
FSM1 Oct 7, 2021
0b7dd95
Merge branch 'dev' into fix/heic-image-preview-fix-1558
FSM1 Oct 7, 2021
2d3f12a
rename variable
FSM1 Oct 7, 2021
ca3038e
Merge branch 'fix/heic-image-preview-fix-1558' of https://github.com/…
FSM1 Oct 7, 2021
cbda122
remove not null check in test
FSM1 Oct 7, 2021
228cacf
resolve test issue, add loader
FSM1 Oct 7, 2021
fd756e1
fix lint
FSM1 Oct 8, 2021
7eb6f52
Merge branch 'dev' into fix/heic-image-preview-fix-1558
FSM1 Oct 8, 2021
e68bc71
update image preview test
asnaith Oct 8, 2021
323928a
use jpg instead of png
FSM1 Oct 11, 2021
5c4eb95
Merge branch 'dev' into fix/heic-image-preview-fix-1558
Tbaut Oct 12, 2021
41589f2
lingui extract
actions-user Oct 12, 2021
a189ac4
Merge branch 'dev' into fix/heic-image-preview-fix-1558
FSM1 Oct 14, 2021
29ac42c
update image preview markup
FSM1 Oct 15, 2021
1f19ad7
fix lint
FSM1 Oct 15, 2021
0c715ad
Merge branch 'dev' into fix/heic-image-preview-fix-1558
FSM1 Oct 18, 2021
4738367
Merge branch 'dev' into fix/heic-image-preview-fix-1558
FSM1 Oct 19, 2021
e8c6d82
Merge branch 'dev' into fix/heic-image-preview-fix-1558
FSM1 Oct 21, 2021
89d05bb
lingui extract
actions-user Oct 21, 2021
1073dcd
fix revocation
FSM1 Oct 21, 2021
27b6162
fix wrong image
FSM1 Oct 21, 2021
daed971
remove unused ref
FSM1 Oct 21, 2021
5b69e05
fix lint
FSM1 Oct 21, 2021
19d101c
Update packages/files-ui/src/Components/Modules/PreviewRenderers/Imag…
FSM1 Oct 22, 2021
9215fcb
Update packages/files-ui/src/Components/Modules/PreviewRenderers/Imag…
FSM1 Oct 22, 2021
f1e4ddc
resolve openssl issue
FSM1 Oct 22, 2021
0e434e3
update remaining build commands
FSM1 Oct 22, 2021
545637d
Merge branch 'dev' into fix/heic-image-preview-fix-1558
FSM1 Oct 22, 2021
72720df
Merge branch 'dev' into fix/heic-image-preview-fix-1558
FSM1 Oct 22, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/files-ui/cypress/tests/file-preview-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,22 @@ describe("File Preview", () => {
homePage.fileItemName().dblclick()
previewModal.unsupportedFileLabel().should("exist")
previewModal.downloadUnsupportedFileButton().should("be.visible")
// ensure that the file download does not start until the download button is clicked
cy.get("@downloadRequest").then(($request) => {
// retrieving the alias (spy) should yield null because posts should not have been made yet
expect($request).to.be.null
})

// begin the file download
previewModal.downloadUnsupportedFileButton().click()

// ensure the download request contains the correct file
cy.get("@downloadRequest").its("request.body").should("contain", {
cy.wait("@downloadRequest").its("request.body").should("contain", {
path: "/file.zip"
})
})

// return to the home and ensure preview menu option is not shown for unsupported file
// return to home, ensure the preview menu option is not shown for an unsupported file
previewModal.closeButton().click()
homePage.fileItemKebabButton().click()
homePage.previewMenuOption().should("not.exist")
Expand Down
1 change: 1 addition & 0 deletions packages/files-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"ethers": "^5.4.3",
"fflate": "^0.7.1",
"formik": "^2.2.5",
"heic-convert": "^1.2.4",
"mime-matcher": "^1.0.5",
"posthog-js": "^1.13.10",
"react": "^16.14.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const SearchFileBrowser: React.FC<IFileBrowserModuleProps> = ({ controls = false
const getPath = useCallback((cid: string): string => {
const searchEntry = getSearchEntry(cid)
// Set like this as look ups should always be using available cids
return searchEntry ? searchEntry.path : ""
return searchEntry ? searchEntry.path.replace(searchEntry.content.name, "") : ""
}, [getSearchEntry])

const pathContents: FileSystemItem[] = useMemo(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const useGetFile = () => {
setError("")

try {

const content = await getFileContent(
id,
{
Expand Down Expand Up @@ -69,8 +68,6 @@ export const useGetFile = () => {
setError(t`There was an error getting the preview.`)
}
}


}, [bucket, getFileContent])

return { getFile, isDownloading, error, downloadProgress }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import { CONTENT_TYPES } from "../../../../Utils/Constants"
import { CSFTheme } from "../../../../Themes/types"
import MimeMatcher from "../../../../Utils/MimeMatcher"
import { useLanguageContext } from "../../../../Contexts/LanguageContext"
import { getPathWithFile } from "../../../../Utils/pathUtils"
import SurveyBanner from "../../../SurveyBanner"
import { DragPreviewLayer } from "./DragPreviewLayer"
import { useFileBrowser } from "../../../../Contexts/FileBrowserContext"
Expand Down Expand Up @@ -1083,7 +1082,7 @@ const FilesList = ({ isShared = false }: Props) => {
closePreview={closePreview}
nextFile={fileIndex < files.length - 1 ? setNextPreview : undefined}
previousFile={fileIndex > 0 ? setPreviousPreview : undefined}
filePath={isSearch && getPath ? getPath(files[fileIndex].cid) : getPathWithFile(currentPath, files[fileIndex].name)}
filePath={isSearch && getPath ? getPath(files[fileIndex].cid) : currentPath}
/>
)}
{ filePath && isReportFileModalOpen &&
Expand Down
51 changes: 30 additions & 21 deletions packages/files-ui/src/Components/Modules/FilePreviewModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import { useFileBrowser } from "../../Contexts/FileBrowserContext"
import { useGetFile } from "./FileBrowsers/hooks/useGetFile"
import { useMemo } from "react"
import Menu from "../../UI-components/Menu"
import { getPathWithFile } from "../../Utils/pathUtils"

export interface IPreviewRendererProps {
contents: Blob
contentType?: string
}

const SUPPORTED_FILE_TYPES: Record<string, React.FC<IPreviewRendererProps>> = {
Expand Down Expand Up @@ -196,6 +198,13 @@ const FilePreviewModal = ({ file, nextFile, previousFile, closePreview, filePath
delta: 20
})

const previewRendererKey = useMemo(() => content_type &&
Object.keys(SUPPORTED_FILE_TYPES).find((type) => {
const matcher = new MimeMatcher(type)

return matcher.match(content_type)
}), [content_type])

useEffect(() => {
let bucketId
// Handle preview in Search where a Bucket is not available, but can be assumed to be a `CSF` bucket
Expand All @@ -205,24 +214,24 @@ const FilePreviewModal = ({ file, nextFile, previousFile, closePreview, filePath
} else {
bucketId = bucket.id
}
getFile({ file, filePath, bucketId })
.then(setFileContent)
.catch(console.error)
}, [file, filePath, getFile, bucket, buckets])

const validRendererMimeType =
content_type &&
Object.keys(SUPPORTED_FILE_TYPES).find((type) => {
const matcher = new MimeMatcher(type)
if (previewRendererKey) {
setFileContent(undefined)
getFile({ file, filePath: getPathWithFile(filePath, file.name), bucketId })
.then((content) => {
setFileContent(content)
})
.catch(console.error)
}
}, [file, filePath, getFile, bucket, buckets, previewRendererKey])


return matcher.match(content_type)
})

const PreviewComponent =
content_type &&
fileContent &&
validRendererMimeType &&
SUPPORTED_FILE_TYPES[validRendererMimeType]
!!content_type &&
!!fileContent &&
!!previewRendererKey &&
SUPPORTED_FILE_TYPES[previewRendererKey]

useHotkeys("Esc,Escape", () => {
if (file) {
Expand All @@ -231,15 +240,11 @@ const FilePreviewModal = ({ file, nextFile, previousFile, closePreview, filePath
})

useHotkeys("Left,ArrowLeft", () => {
if (file && previousFile) {
previousFile()
}
previousFile && previousFile()
})

useHotkeys("Right,ArrowRight", () => {
if (file && nextFile) {
nextFile()
}
nextFile && nextFile()
})

const handleDownload = useCallback(() => {
Expand Down Expand Up @@ -389,7 +394,11 @@ const FilePreviewModal = ({ file, nextFile, previousFile, closePreview, filePath
!error &&
compatibleFilesMatcher.match(content_type) &&
fileContent &&
PreviewComponent && <PreviewComponent contents={fileContent} />}
PreviewComponent &&
<PreviewComponent
contents={fileContent}
contentType={content_type} />
}
</div>
</Grid>
{desktop && (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { useEffect, useState } from "react"
import { IPreviewRendererProps } from "../FilePreviewModal"
import { TransformWrapper, TransformComponent } from "react-zoom-pan-pinch"
import heicConvert from "heic-convert"

import {
makeStyles,
ITheme,
Expand All @@ -11,7 +13,8 @@ import {
Button,
ZoomInIcon,
ZoomOutIcon,
FullscreenIcon
FullscreenIcon,
Loading
} from "@chainsafe/common-components"

const useStyles = makeStyles(
Expand Down Expand Up @@ -41,67 +44,82 @@ const useStyles = makeStyles(
})
)

const ImagePreview: React.FC<IPreviewRendererProps> = ({ contents }) => {
const ImagePreview: React.FC<IPreviewRendererProps> = ({ contents, contentType }) => {
const [imageUrl, setImageUrl] = useState<string | undefined>()

const [loading, setLoading] = useState(false)
useEffect(() => {
setImageUrl(URL.createObjectURL(contents))

return () => {
imageUrl && URL.revokeObjectURL(imageUrl)
if (contentType !== "image/heic") {
setImageUrl(URL.createObjectURL(contents))
} else {
setLoading(true)
contents.arrayBuffer()
.then(b => heicConvert({
buffer: Buffer.from(b),
format: "JPEG",
quality: 0.5
}))
.catch(console.error)
.then(c => setImageUrl(URL.createObjectURL(new Blob([c]))))
.finally(() => setLoading(false))
}
// eslint-disable-next-line
}, [contents])
}, [contents, contentType])

const classes = useStyles()
const { desktop } = useThemeSwitcher()

return (
<div className={classes.root}>
<TransformWrapper
options={{
limitToBounds: true,
limitToWrapper: true,
minScale: 0.2
}}
>
{
{loading
? <Loading
size={50}
type='inherit'
/>
: <TransformWrapper
options={{
limitToBounds: true,
limitToWrapper: true,
minScale: 0.2
}}
>
{
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
({ zoomIn, zoomOut, resetTransform }) => (
<>
{desktop && (
<div className={classes.controlsContainer}>
<Button
onClick={zoomIn}
data-cy="button-zoom-in"
>
<ZoomInIcon />
</Button>
<Button
onClick={zoomOut}
data-cy="button-zoom-out"
>
<ZoomOutIcon />
</Button>
<Button
onClick={resetTransform}
data-cy="button-full-screen"
>
<FullscreenIcon />
</Button>
</div>
)}
<TransformComponent>
<img
src={imageUrl}
alt=""
className={classes.root}
/>
</TransformComponent>
</>
)
}
</TransformWrapper>
({ zoomIn, zoomOut, resetTransform }) => (
<>
{desktop && (
<div className={classes.controlsContainer}>
<Button
onClick={zoomIn}
data-cy="button-zoom-in"
>
<ZoomInIcon />
</Button>
<Button
onClick={zoomOut}
data-cy="button-zoom-out"
>
<ZoomOutIcon />
</Button>
<Button
onClick={resetTransform}
data-cy="button-full-screen"
>
<FullscreenIcon />
</Button>
</div>
)}
<TransformComponent>
<img
src={imageUrl}
alt=""
className={classes.root}
onLoad={() => imageUrl && URL.revokeObjectURL(imageUrl)} />
</TransformComponent>
</>
)
}
</TransformWrapper>
}
</div>
)
}
Expand Down
5 changes: 3 additions & 2 deletions packages/files-ui/src/Utils/contentTypeGuesser.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const guessContentType = (fileName: string) => {
const { length, [length - 1]: ext } = fileName.split(".")

switch (ext) {
switch (ext.toLowerCase()) {
case "pdf":
return "application/pdf"
case "jpg":
case "jpeg":
case "png":
case "gif":
case "bmp":
return `image/${ext}`
case "heic":
return `image/${ext.toLowerCase()}`
case "mp3":
case "m4a":
return `audio/${ext}`
Expand Down
1 change: 1 addition & 0 deletions packages/files-ui/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "heic-convert";
Loading