From fd53c0b9b8a4dde16415663abd46cc63aaf187be Mon Sep 17 00:00:00 2001 From: Pushkar Anand Date: Sat, 5 Jun 2021 20:33:54 +0530 Subject: [PATCH] Show file info --- public/info_icon.png | Bin 0 -> 472 bytes src/components/PhotoSwipe/PhotoSwipe.tsx | 281 +++++++++++++++++------ src/pages/_app.tsx | 14 +- src/utils/file/index.ts | 23 ++ src/utils/strings/englishConstants.tsx | 16 ++ 5 files changed, 262 insertions(+), 72 deletions(-) create mode 100644 public/info_icon.png diff --git a/public/info_icon.png b/public/info_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..7d0740a8f18abeb48ffd30c10f6bb1dd02bf3d50 GIT binary patch literal 472 zcmV;}0Vn>6P)Nkl-~zyf0|_A>t@^=7tT47kdJH#yB{P@d?=Tdc&;gn1=7N$t=7P&@06C2)JdC8@gt@-)8a~ z;b_qgQKn8?gHvRZb7N|B;%Vj_>niYEL65k`HFG(tb#onX$X)H;{peb_>J)c|<*-=g zzA!m*lO>6gwTR1AD=@xYuU}IAaJ;*fMt=p%_(&+N>ivM5Q0St zIML2KJ*C?I*WIhH)a~V+Vd~n|>b|=)N|<*gsVk^O5TsF(yh}>OuSMWXndV(mViV2s ziXscaM9MAik{amMBIrpi`~pGaDKaRs0fDB-2G`VpKyytEgl0gX5t>1(sO&}BR?&2` zYrI`scyJnjGA%s%jgKuB1}_VvW8=fGh4J0O2gJ%3%EBkm$~X4kEZ7h6yOUA!_;^?V O0000 ( + <> + {label} + {value} + +); + +function ExifData(props: { exif: any }) { + const { exif } = props; + const [showAll, setShowAll] = useState(false); + + const changeHandler = (e: React.ChangeEvent) => { + setShowAll(e.target.checked); + }; + + const renderAllValues = () => (
{exif.raw}
); + + const renderSelectedValues = () => (<> + {exif?.Make && exif?.Model && renderInfoItem(constants.DEVICE, `${exif.Make} ${exif.Model}`)} + {exif?.ImageWidth && exif?.ImageHeight && renderInfoItem(constants.IMAGE_SIZE, `${exif.ImageWidth} x ${exif.ImageHeight}`)} + {exif?.Flash && renderInfoItem(constants.FLASH, exif.Flash)} + {exif?.FocalLength && renderInfoItem(constants.FOCAL_LENGTH, exif.FocalLength.toString())} + {exif?.ApertureValue && renderInfoItem(constants.APERTURE, exif.ApertureValue.toString())} + {exif?.ISOSpeedRatings && renderInfoItem(constants.ISO, exif.ISOSpeedRatings.toString())} + ); + + return (<> + + {constants.EXIF} + + + + {constants.SHOW_ALL} + + + + {showAll ? renderAllValues() : renderSelectedValues()} + ); +} + function PhotoSwipe(props: Iprops) { const pswpElement = useRef(); const [photoSwipe, setPhotoSwipe] = useState>(); const { isOpen, items } = props; const [isFav, setIsFav] = useState(false); + const [showInfo, setShowInfo] = useState(false); + const [metadata, setMetaData] = useState(null); + const [exif, setExif] = useState(null); const needUpdate = useRef(false); useEffect(() => { @@ -69,14 +135,18 @@ function PhotoSwipe(props: Iprops) { return item.initialZoomLevel < 0.7 ? 1 : 1.5; }, getThumbBoundsFn: (index) => { - const file = items[index]; - const ele = document.getElementById(`thumb-${file.id}`); - if (ele) { - const rect = ele.getBoundingClientRect(); - const pageYScroll = window.pageYOffset || document.documentElement.scrollTop; - return { x: rect.left, y: rect.top + pageYScroll, w: rect.width }; + try { + const file = items[index]; + const ele = document.getElementById(`thumb-${file.id}`); + if (ele) { + const rect = ele.getBoundingClientRect(); + const pageYScroll = window.pageYOffset || document.documentElement.scrollTop; + return { x: rect.left, y: rect.top + pageYScroll, w: rect.width }; + } + return null; + } catch (e) { + return null; } - return null; }, }; const photoSwipe = new Photoswipe( @@ -100,6 +170,7 @@ function PhotoSwipe(props: Iprops) { } }); photoSwipe.listen('beforeChange', updateFavButton); + photoSwipe.listen('resize', checkExifAvailable); photoSwipe.init(); needUpdate.current = false; setPhotoSwipe(photoSwipe); @@ -151,6 +222,37 @@ function PhotoSwipe(props: Iprops) { } }; + const checkExifAvailable = () => { + setExif(null); + setTimeout(() => { + const img = document.querySelector('.pswp__img:not(.pswp__img--placeholder)'); + if (img) { + // @ts-expect-error + EXIF.getData(img, function() { + const exif = EXIF.getAllTags(this); + exif.raw = EXIF.pretty(this); + if (exif.raw) { + setExif(exif); + } + }); + } + }, 100); + }; + + const showExif = () => { + const file:File = items[photoSwipe?.getCurrentIndex()]; + if (file.metadata) { + setMetaData(file.metadata); + setExif(null); + checkExifAvailable(); + setShowInfo(true); + } + }; + + const handleCloseInfo = () => { + setShowInfo(false); + }; + const downloadFile = async (file) => { const { loadingBar } = props; const a = document.createElement('a'); @@ -167,76 +269,119 @@ function PhotoSwipe(props: Iprops) { let { className } = props; className = classnames(['pswp', className]).trim(); return ( -