Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3351878
D8324.id28228.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
D8324.id28228.diff
View Options
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<boolean>,
- // returns URI if holder cached or null if not
- +getCachedFile: (holder: string) => Promise<?string>,
- // returns URI of saved file
- +saveFile: (holder: string, uri: string) => Promise<string>,
+ +hasURI: (mediaURI: string) => Promise<boolean>,
+ // returns URI if blob URI is cached or null if not
+ +getCachedFile: (blobURI: string) => Promise<?string>,
+ // 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<string>,
// clears cache (deletes all files)
+clearCache: () => Promise<void>,
// 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<?string>,
+ +get: (blobURI: string) => Promise<?string>,
/**
- * 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<void>,
+ +set: (blobURI: string, mediaURI: string) => Promise<void>,
/**
* 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<string, string>();
- async function get(holder: string): Promise<?string> {
- const cachedURI = uriCache.get(holder);
- if (cachedURI) {
+ async function get(blobURI: string): Promise<?string> {
+ 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<void> {
- const cachedURI = await persistence.saveFile(holder, uri);
- uriCache.set(holder, cachedURI);
+ async function set(blobURI: string, mediaURI: string): Promise<void> {
+ 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<boolean> {
- const path = pathFromURI(uri);
+async function hasURI(mediaURI: string): Promise<boolean> {
+ 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<string> {
+async function saveFile(blobURI: string, mediaURI: string): Promise<string> {
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}`;
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 24, 3:55 AM (19 h, 40 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2573794
Default Alt Text
D8324.id28228.diff (6 KB)
Attached To
Mode
D8324: [lib][native] Disambiguate URIs in media-cache
Attached
Detach File
Event Timeline
Log In to Comment