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 { 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 || '') }))

View File

@ -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 },

View File

@ -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)

View File

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

View File

@ -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
}

View File

@ -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)