From c3fb1e20382623ab05be00c63c5c1b4b2ae86ea9 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Mon, 15 Mar 2021 17:25:13 -0700 Subject: [PATCH] fix: media cache should be behind async db API (#1999) --- src/routes/_actions/compose.js | 3 +-- src/routes/_actions/media.js | 4 ++-- .../_components/dialog/components/MediaAltEditor.html | 4 ++-- src/routes/_database/databaseApis.js | 1 + src/routes/{_utils => _database}/mediaUploadFileCache.js | 2 +- tests/unit/test-media-cache.js | 8 ++++---- 6 files changed, 11 insertions(+), 11 deletions(-) rename src/routes/{_utils => _database}/mediaUploadFileCache.js (97%) diff --git a/src/routes/_actions/compose.js b/src/routes/_actions/compose.js index f7d74164..74842d1b 100644 --- a/src/routes/_actions/compose.js +++ b/src/routes/_actions/compose.js @@ -6,7 +6,6 @@ import { database } from '../_database/database' import { emit } from '../_utils/eventBus' import { putMediaMetadata } from '../_api/media' import uniqBy from 'lodash-es/uniqBy' -import { deleteCachedMediaFile } from '../_utils/mediaUploadFileCache' import { scheduleIdleTask } from '../_utils/scheduleIdleTask' import { formatIntl } from '../_utils/formatIntl' @@ -61,7 +60,7 @@ export async function postStatus (realm, text, inReplyToId, mediaIds, addStatusOrNotification(currentInstance, 'home', status) store.clearComposeData(realm) emit('postedStatus', realm, inReplyToUuid) - scheduleIdleTask(() => (mediaIds || []).forEach(mediaId => deleteCachedMediaFile(mediaId))) // clean up media cache + scheduleIdleTask(() => (mediaIds || []).forEach(mediaId => database.deleteCachedMediaFile(mediaId))) // clean up media cache } catch (e) { console.error(e) /* no await */ toast.say(formatIntl('intl.unableToPost', { error: (e.message || '') })) diff --git a/src/routes/_actions/media.js b/src/routes/_actions/media.js index 9a87696a..48e3f363 100644 --- a/src/routes/_actions/media.js +++ b/src/routes/_actions/media.js @@ -2,8 +2,8 @@ import { store } from '../_store/store' import { uploadMedia } from '../_api/media' import { toast } from '../_components/toast/toast' import { scheduleIdleTask } from '../_utils/scheduleIdleTask' -import { setCachedMediaFile } from '../_utils/mediaUploadFileCache' import { formatIntl } from '../_utils/formatIntl' +import { database } from '../_database/database' export async function doMediaUpload (realm, file) { const { currentInstance, accessToken } = store.get() @@ -14,7 +14,7 @@ export async function doMediaUpload (realm, file) { if (composeMedia.length === 4) { throw new Error('Only 4 media max are allowed') } - await setCachedMediaFile(response.id, file) + await database.setCachedMediaFile(response.id, file) composeMedia.push({ data: response, file: { name: file.name }, diff --git a/src/routes/_components/dialog/components/MediaAltEditor.html b/src/routes/_components/dialog/components/MediaAltEditor.html index 18a3d814..2a154fa7 100644 --- a/src/routes/_components/dialog/components/MediaAltEditor.html +++ b/src/routes/_components/dialog/components/MediaAltEditor.html @@ -106,8 +106,8 @@ import { runTesseract } from '../../../_utils/runTesseract' import SvgIcon from '../../SvgIcon.html' import { toast } from '../../toast/toast' - import { getCachedMediaFile } from '../../../_utils/mediaUploadFileCache' import { formatIntl } from '../../../_utils/formatIntl' + import { database } from '../../../_database/database' const updateRawTextInStore = throttleTimer(requestPostAnimationFrame) @@ -191,7 +191,7 @@ this.set({ extractionProgress: progress * 100 }) }) } - const file = await getCachedMediaFile(mediaId) + const file = await database.getCachedMediaFile(mediaId) let text if (file) { // Avoid downloading from the network a file that the user *just* uploaded const fileUrl = URL.createObjectURL(file) diff --git a/src/routes/_database/databaseApis.js b/src/routes/_database/databaseApis.js index 4a39760a..dcd6c30f 100644 --- a/src/routes/_database/databaseApis.js +++ b/src/routes/_database/databaseApis.js @@ -10,3 +10,4 @@ export { insertTimelineItems, insertStatus } from './timelines/insertion' export * from './meta' export * from './relationships' export * from './webShare' +export * from './mediaUploadFileCache' diff --git a/src/routes/_utils/mediaUploadFileCache.js b/src/routes/_database/mediaUploadFileCache.js similarity index 97% rename from src/routes/_utils/mediaUploadFileCache.js rename to src/routes/_database/mediaUploadFileCache.js index 143799ea..005c43b5 100644 --- a/src/routes/_utils/mediaUploadFileCache.js +++ b/src/routes/_database/mediaUploadFileCache.js @@ -79,6 +79,6 @@ export async function getAllCachedFileIds () { return (await getAllKeys()).map(keyToData).map(_ => _[1]) } -export function setDeleteAfter (newDeleteAfter) { +export function setDeleteCachedMediaFilesAfter (newDeleteAfter) { deleteAfter = newDeleteAfter } diff --git a/tests/unit/test-media-cache.js b/tests/unit/test-media-cache.js index 1ebf671d..7612f326 100644 --- a/tests/unit/test-media-cache.js +++ b/tests/unit/test-media-cache.js @@ -3,8 +3,8 @@ import '../indexedDBShims' import assert from 'assert' import { - getCachedMediaFile, setCachedMediaFile, deleteCachedMediaFile, getAllCachedFileIds, setDeleteAfter, DELETE_AFTER -} from '../../src/routes/_utils/mediaUploadFileCache' + getCachedMediaFile, setCachedMediaFile, deleteCachedMediaFile, getAllCachedFileIds, setDeleteCachedMediaFilesAfter, DELETE_AFTER +} from '../../src/routes/_database/mediaUploadFileCache' describe('test-database.js', function () { this.timeout(60000) @@ -13,7 +13,7 @@ describe('test-database.js', function () { for (const key of await getAllCachedFileIds()) { await deleteCachedMediaFile(key) } - setDeleteAfter(DELETE_AFTER) + setDeleteCachedMediaFilesAfter(DELETE_AFTER) }) it('can store media files', async () => { @@ -51,7 +51,7 @@ describe('test-database.js', function () { }) it('deletes old files during set()', async () => { - setDeleteAfter(0) + setDeleteCachedMediaFilesAfter(0) await setCachedMediaFile('woot', 'woot') await setCachedMediaFile('woot2', 'woot2') assert.deepStrictEqual(await getCachedMediaFile('woot'), undefined)