diff --git a/lib/components/media-cache-provider.react.js b/lib/components/media-cache-provider.react.js --- a/lib/components/media-cache-provider.react.js +++ b/lib/components/media-cache-provider.react.js @@ -8,11 +8,12 @@ */ export type MediaCachePersistence = { // returns true if the URI is a cached media URI. This check should be fast - +hasURI: (uri: string) => Promise, - // returns URI if holder cached or null if not - +getCachedFile: (holder: string) => Promise, - // returns URI of saved file - +saveFile: (holder: string, uri: string) => Promise, + +hasURI: (mediaURI: string) => Promise, + // returns URI if blob URI is cached or null if not + +getCachedFile: (blobURI: string) => Promise, + // returns URI of saved file. Blob URI is the cache key, media URI is the + // media content URI (either file or data-uri) + +saveFile: (blobURI: string, mediaURI: string) => Promise, // clears cache (deletes all files) +clearCache: () => Promise, // returns size of cache in bytes @@ -27,13 +28,13 @@ type MediaCacheContextType = { /** - * Gets the URI for a given holder, or `null` if it's not cached. + * Gets the media URI for a given blob URI, or `null` if it's not cached. */ - +get: (holder: string) => Promise, + +get: (blobURI: string) => Promise, /** - * Saves the URI for a given holder. Accepts both file and data URIs. + * Saves the media URI for a given blob URI. Accepts both file and data URIs. */ - +set: (holder: string, uri: string) => Promise, + +set: (blobURI: string, mediaURI: string) => Promise, /** * Clears the in-memory cache and cleans up old files from the platform cache. * This should be called when no media components are mounted. @@ -48,29 +49,29 @@ // holder -> URI const uriCache = new Map(); - async function get(holder: string): Promise { - const cachedURI = uriCache.get(holder); - if (cachedURI) { + async function get(blobURI: string): Promise { + const cachedMediaURI = uriCache.get(blobURI); + if (cachedMediaURI) { // even though we have the URI in memory, we still need to check if it's // still valid (e.g. file was deleted from the platform cache) - const uriExists = await persistence.hasURI(cachedURI); + const uriExists = await persistence.hasURI(cachedMediaURI); if (uriExists) { - return cachedURI; + return cachedMediaURI; } else { - uriCache.delete(holder); + uriCache.delete(blobURI); } } // if the in-memory cache doesn't have it, check the platform cache - const cachedFile = await persistence.getCachedFile(holder); + const cachedFile = await persistence.getCachedFile(blobURI); if (cachedFile) { - uriCache.set(holder, cachedFile); + uriCache.set(blobURI, cachedFile); } return cachedFile; } - async function set(holder: string, uri: string): Promise { - const cachedURI = await persistence.saveFile(holder, uri); - uriCache.set(holder, cachedURI); + async function set(blobURI: string, mediaURI: string): Promise { + const cachedURI = await persistence.saveFile(blobURI, mediaURI); + uriCache.set(blobURI, cachedURI); } async function evictCache() { diff --git a/native/media/media-cache.js b/native/media/media-cache.js --- a/native/media/media-cache.js +++ b/native/media/media-cache.js @@ -17,10 +17,10 @@ const cacheDirectory = `${temporaryDirectoryPath}media-cache`; -function basenameFromHolder(holder: string) { - // if holder is a file URI or path, use the last segment of the path - const holderBase = holder.split('/').pop(); - return filenameWithoutExtension(holderBase); +function basenameFromBlobURI(blobURI: string) { + // if blobURI is a file URI or path, use the last segment of the path + const filename = blobURI.split('/').pop(); + return filenameWithoutExtension(filename); } async function ensureCacheDirectory() { @@ -40,19 +40,19 @@ return files.reduce((total, file) => total + file.size, 0); } -async function hasURI(uri: string): Promise { - const path = pathFromURI(uri); +async function hasURI(mediaURI: string): Promise { + const path = pathFromURI(mediaURI); if (!path) { return false; } return await fs.exists(path); } -async function getCachedFile(holder: string) { +async function getCachedFile(blobURI: string) { const cachedFiles = await listCachedFiles(); - const baseHolder = basenameFromHolder(holder); + const basename = basenameFromBlobURI(blobURI); const cachedFile = cachedFiles.find(file => - filenameWithoutExtension(file).startsWith(baseHolder), + filenameWithoutExtension(file).startsWith(basename), ); if (cachedFile) { return `file://${cacheDirectory}/${cachedFile}`; @@ -70,28 +70,28 @@ } const dataURLRegex = /^data:([^;]+);base64,([a-zA-Z0-9+/]+={0,2})$/; -async function saveFile(holder: string, uri: string): Promise { +async function saveFile(blobURI: string, mediaURI: string): Promise { await ensureCacheDirectory(); let filePath; - const baseHolder = basenameFromHolder(holder); - const isDataURI = uri.startsWith('data:'); + const basename = basenameFromBlobURI(blobURI); + const isDataURI = mediaURI.startsWith('data:'); if (isDataURI) { - const [, mime, data] = uri.match(dataURLRegex) ?? []; + const [, mime, data] = mediaURI.match(dataURLRegex) ?? []; invariant(mime, 'malformed data-URI: missing MIME type'); invariant(data, 'malformed data-URI: invalid data'); - const filename = readableFilename(baseHolder, mime) ?? baseHolder; + const filename = readableFilename(basename, mime) ?? basename; filePath = `${cacheDirectory}/${filename}`; await fs.writeFile(filePath, data, 'base64'); } else { - const uriFilename = filenameFromPathOrURI(uri); + const uriFilename = filenameFromPathOrURI(mediaURI); invariant(uriFilename, 'malformed URI: missing filename'); const extension = extensionFromFilename(uriFilename); const filename = extension - ? replaceExtension(baseHolder, extension) - : baseHolder; + ? replaceExtension(basename, extension) + : basename; filePath = `${cacheDirectory}/${filename}`; - await fs.copyFile(uri, filePath); + await fs.copyFile(mediaURI, filePath); } return `file://${filePath}`; }