fix: media cache should be behind async db API (#1999)

This commit is contained in:
Nolan Lawson 2021-03-15 17:25:13 -07:00 committed by GitHub
parent c4e73106cf
commit c3fb1e2038
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 11 additions and 11 deletions

View file

@ -6,7 +6,6 @@ import { database } from '../_database/database'
import { emit } from '../_utils/eventBus' import { emit } from '../_utils/eventBus'
import { putMediaMetadata } from '../_api/media' import { putMediaMetadata } from '../_api/media'
import uniqBy from 'lodash-es/uniqBy' import uniqBy from 'lodash-es/uniqBy'
import { deleteCachedMediaFile } from '../_utils/mediaUploadFileCache'
import { scheduleIdleTask } from '../_utils/scheduleIdleTask' import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
import { formatIntl } from '../_utils/formatIntl' import { formatIntl } from '../_utils/formatIntl'
@ -61,7 +60,7 @@ export async function postStatus (realm, text, inReplyToId, mediaIds,
addStatusOrNotification(currentInstance, 'home', status) addStatusOrNotification(currentInstance, 'home', status)
store.clearComposeData(realm) store.clearComposeData(realm)
emit('postedStatus', realm, inReplyToUuid) 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) { } catch (e) {
console.error(e) console.error(e)
/* no await */ toast.say(formatIntl('intl.unableToPost', { error: (e.message || '') })) /* no await */ toast.say(formatIntl('intl.unableToPost', { error: (e.message || '') }))

View file

@ -2,8 +2,8 @@ import { store } from '../_store/store'
import { uploadMedia } from '../_api/media' import { uploadMedia } from '../_api/media'
import { toast } from '../_components/toast/toast' import { toast } from '../_components/toast/toast'
import { scheduleIdleTask } from '../_utils/scheduleIdleTask' import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
import { setCachedMediaFile } from '../_utils/mediaUploadFileCache'
import { formatIntl } from '../_utils/formatIntl' import { formatIntl } from '../_utils/formatIntl'
import { database } from '../_database/database'
export async function doMediaUpload (realm, file) { export async function doMediaUpload (realm, file) {
const { currentInstance, accessToken } = store.get() const { currentInstance, accessToken } = store.get()
@ -14,7 +14,7 @@ export async function doMediaUpload (realm, file) {
if (composeMedia.length === 4) { if (composeMedia.length === 4) {
throw new Error('Only 4 media max are allowed') throw new Error('Only 4 media max are allowed')
} }
await setCachedMediaFile(response.id, file) await database.setCachedMediaFile(response.id, file)
composeMedia.push({ composeMedia.push({
data: response, data: response,
file: { name: file.name }, file: { name: file.name },

View file

@ -106,8 +106,8 @@
import { runTesseract } from '../../../_utils/runTesseract' import { runTesseract } from '../../../_utils/runTesseract'
import SvgIcon from '../../SvgIcon.html' import SvgIcon from '../../SvgIcon.html'
import { toast } from '../../toast/toast' import { toast } from '../../toast/toast'
import { getCachedMediaFile } from '../../../_utils/mediaUploadFileCache'
import { formatIntl } from '../../../_utils/formatIntl' import { formatIntl } from '../../../_utils/formatIntl'
import { database } from '../../../_database/database'
const updateRawTextInStore = throttleTimer(requestPostAnimationFrame) const updateRawTextInStore = throttleTimer(requestPostAnimationFrame)
@ -191,7 +191,7 @@
this.set({ extractionProgress: progress * 100 }) this.set({ extractionProgress: progress * 100 })
}) })
} }
const file = await getCachedMediaFile(mediaId) const file = await database.getCachedMediaFile(mediaId)
let text let text
if (file) { // Avoid downloading from the network a file that the user *just* uploaded if (file) { // Avoid downloading from the network a file that the user *just* uploaded
const fileUrl = URL.createObjectURL(file) const fileUrl = URL.createObjectURL(file)

View file

@ -10,3 +10,4 @@ export { insertTimelineItems, insertStatus } from './timelines/insertion'
export * from './meta' export * from './meta'
export * from './relationships' export * from './relationships'
export * from './webShare' export * from './webShare'
export * from './mediaUploadFileCache'

View file

@ -79,6 +79,6 @@ export async function getAllCachedFileIds () {
return (await getAllKeys()).map(keyToData).map(_ => _[1]) return (await getAllKeys()).map(keyToData).map(_ => _[1])
} }
export function setDeleteAfter (newDeleteAfter) { export function setDeleteCachedMediaFilesAfter (newDeleteAfter) {
deleteAfter = newDeleteAfter deleteAfter = newDeleteAfter
} }

View file

@ -3,8 +3,8 @@
import '../indexedDBShims' import '../indexedDBShims'
import assert from 'assert' import assert from 'assert'
import { import {
getCachedMediaFile, setCachedMediaFile, deleteCachedMediaFile, getAllCachedFileIds, setDeleteAfter, DELETE_AFTER getCachedMediaFile, setCachedMediaFile, deleteCachedMediaFile, getAllCachedFileIds, setDeleteCachedMediaFilesAfter, DELETE_AFTER
} from '../../src/routes/_utils/mediaUploadFileCache' } from '../../src/routes/_database/mediaUploadFileCache'
describe('test-database.js', function () { describe('test-database.js', function () {
this.timeout(60000) this.timeout(60000)
@ -13,7 +13,7 @@ describe('test-database.js', function () {
for (const key of await getAllCachedFileIds()) { for (const key of await getAllCachedFileIds()) {
await deleteCachedMediaFile(key) await deleteCachedMediaFile(key)
} }
setDeleteAfter(DELETE_AFTER) setDeleteCachedMediaFilesAfter(DELETE_AFTER)
}) })
it('can store media files', async () => { it('can store media files', async () => {
@ -51,7 +51,7 @@ describe('test-database.js', function () {
}) })
it('deletes old files during set()', async () => { it('deletes old files during set()', async () => {
setDeleteAfter(0) setDeleteCachedMediaFilesAfter(0)
await setCachedMediaFile('woot', 'woot') await setCachedMediaFile('woot', 'woot')
await setCachedMediaFile('woot2', 'woot2') await setCachedMediaFile('woot2', 'woot2')
assert.deepStrictEqual(await getCachedMediaFile('woot'), undefined) assert.deepStrictEqual(await getCachedMediaFile('woot'), undefined)