Skip to content

Commit 8985fff

Browse files
committed
Revoke some blob URLs properly
#224
1 parent f166d89 commit 8985fff

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

src/lib/components/assets/shared/asset-preview.svelte

+7-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
* Update the {@link loaded} state when the media is loaded.
107107
*/
108108
const checkLoaded = async () => {
109-
if (!mediaElement) {
109+
if (!mediaElement || !src) {
110110
return;
111111
}
112112
@@ -133,10 +133,16 @@
133133
}
134134
135135
loaded = true;
136+
137+
// Revoke the thumbnail blob URL
138+
if (asset && isThumbnail && src?.startsWith('blob:')) {
139+
URL.revokeObjectURL(src);
140+
}
136141
};
137142
138143
$: {
139144
void mediaElement;
145+
void src;
140146
checkLoaded();
141147
}
142148
</script>

src/lib/services/assets/index.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,6 @@ let thumbnailDB = undefined;
205205
* @returns {Promise<string | undefined>} Thumbnail blob URL.
206206
*/
207207
export const getAssetThumbnailURL = async (asset) => {
208-
// Use a cached image if available
209-
if (asset.thumbnailURL) {
210-
return asset.thumbnailURL;
211-
}
212-
213208
const isPDF = asset.name.endsWith('.pdf');
214209

215210
if (!(['image', 'video'].includes(asset.kind) || isPDF)) {
@@ -235,12 +230,7 @@ export const getAssetThumbnailURL = async (asset) => {
235230
await thumbnailDB?.set(asset.sha, thumbnailBlob);
236231
}
237232

238-
const thumbnailURL = URL.createObjectURL(thumbnailBlob);
239-
240-
// Cache the image as blob URL for later use
241-
asset.thumbnailURL = thumbnailURL;
242-
243-
return thumbnailURL;
233+
return URL.createObjectURL(thumbnailBlob);
244234
};
245235

246236
/**

src/lib/services/utils/media.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,13 @@ export const renderPDF = async (
200200
}
201201
}
202202

203+
const blobURL = URL.createObjectURL(blob);
203204
const canvas = new OffscreenCanvas(512, 512);
204205
const context = /** @type {OffscreenCanvasRenderingContext2D} */ (canvas.getContext('2d'));
205206

206207
try {
207208
const pdfDocument = await pdfjs.getDocument({
208-
url: URL.createObjectURL(blob),
209+
url: blobURL,
209210
isEvalSupported: false,
210211
disableAutoFetch: true,
211212
}).promise;
@@ -219,6 +220,8 @@ export const renderPDF = async (
219220
canvasContext: context,
220221
viewport: scale === 1 ? viewport : pdfPage.getViewport({ scale }),
221222
}).promise;
223+
224+
URL.revokeObjectURL(blobURL);
222225
} catch {
223226
throw new Error('Failed to render PDF');
224227
}

src/lib/typedefs.js

-2
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,6 @@
900900
* @property {File} [file] - File object. Local backend only.
901901
* @property {string} [blobURL] - Blob URL for the asset. It’s a temporary URL for a remote file
902902
* being fetched or a local file being uploaded. Or `undefined` if the URL is not generated yet.
903-
* @property {string} [thumbnailURL] - Thumbnail Blob URL for the asset. Used to cache a rendered
904-
* thumbnail of a PDF document.
905903
* @property {string} name - File name.
906904
* @property {string} path - File path.
907905
* @property {string} sha - SHA-1 hash for the file.

0 commit comments

Comments
 (0)