Skip to content

Commit eefae25

Browse files
authored
fix(files): avoid duplicated fetch during preview (#2254)
Closes #2217
1 parent b9f622d commit eefae25

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

public/locales/en/files.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
"paragraph3": "Pro tip: drag and drop a file from any other page of the Web UI to add them to the root of your MFS."
102102
}
103103
},
104-
"loadMore": "Load more",
104+
"previewLimitReached": "This preview is limited to 10 KiB. Click the download button to access the full file.",
105105
"previousFolder": "Go back to previous folder",
106106
"fileLabel": "Select {type} {name} with size: {size}",
107107
"hashUnavailable": "hash unavailable",

src/bundles/files/actions.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,13 @@ const actions = () => ({
214214
...fileFromStats({ ...stats, path }),
215215
fetched: time,
216216
type: 'file',
217-
read: () => ipfs.cat(stats.cid),
217+
/**
218+
* Reads a portion of data from IPFS.
219+
* @param {number} offset - The starting point to read from.
220+
* @param {number} length - The number of bytes to read.
221+
* @returns {AsyncIterable<Uint8Array>} An async generator that yields the data read from IPFS.
222+
*/
223+
read: (offset, length) => ipfs.cat(stats.cid, { offset, length }),
218224
name: path.split('/').pop(),
219225
size: stats.size,
220226
cid: stats.cid

src/files/file-preview/FilePreview.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { useDrag } from 'react-dnd'
1111
import { toString as fromUint8ArrayToString } from 'uint8arrays'
1212
import Button from '../../components/button/Button.js'
1313

14+
const maxPlainTextPreview = 1024 * 10 // only preview small part of huge files
15+
1416
const Drag = ({ name, size, cid, path, children }) => {
1517
const [, drag] = useDrag({
1618
item: { name, size, cid, path, type: 'FILE' }
@@ -29,7 +31,11 @@ const Preview = (props) => {
2931
const type = typeFromExt(name)
3032

3133
const loadContent = useCallback(async () => {
32-
const readBuffer = buffer || await read()
34+
if (['audio', 'video', 'pdf', 'image'].includes(type)) {
35+
// noop, we dont need to read() preview for these because we embed them on page
36+
return
37+
}
38+
const readBuffer = buffer || await read(0, maxPlainTextPreview)
3339
if (!buffer) {
3440
setBuffer(readBuffer)
3541
}
@@ -44,7 +50,7 @@ const Preview = (props) => {
4450
const hasMore = !done && new TextEncoder().encode(currentContent).length < size
4551

4652
setHasMoreContent(hasMore)
47-
}, [buffer, content, read, size])
53+
}, [buffer, content, read, size, type])
4854

4955
useEffect(() => {
5056
loadContent()
@@ -102,21 +108,20 @@ const Preview = (props) => {
102108
: <Trans i18nKey='openWithLocalAndPublicGateway' t={t}>
103109
Try opening it instead with your <a href={src} download target='_blank' rel='noopener noreferrer' className='link blue'>local gateway</a> or <a href={srcPublic} download target='_blank' rel='noopener noreferrer' className='link blue'>public gateway</a>.
104110
</Trans>
105-
106111
}
107-
108112
</p>
109113
</div>
110114
)
111115

112-
if (size > 1024 * 1024 * 4) {
113-
return cantPreview
114-
}
115-
116116
if (content === null) {
117117
return <ComponentLoader />
118118
}
119119

120+
// a precaution to not render too much, in case we overread
121+
if (content.length > maxPlainTextPreview) {
122+
return cantPreview
123+
}
124+
120125
if (isBinary(name, content)) {
121126
return cantPreview
122127
}
@@ -126,12 +131,12 @@ const Preview = (props) => {
126131
{content}
127132
</pre>
128133
{ hasMoreContent && <div className="w-100 flex items-center justify-center">
129-
<Button onClick={ loadContent }>
130-
{ t('loadMore')}
131-
</Button>
132-
<Button className="mh2" onClick={ onDownload }>
134+
<p><Trans i18nKey='previewLimitReached' t={t}>This preview is limited to 10 KiB. Click the download button to access the full file.</Trans></p>
135+
<p>
136+
<Button className="mh2 lh-copy bn justify-center flex " onClick={ onDownload }>
133137
{ t('app:actions.download')}
134138
</Button>
139+
</p>
135140
</div>}
136141
</>
137142
}

0 commit comments

Comments
 (0)