1
Fork 0

Correctly cleanup albums from cache

This commit is contained in:
viktorstrate 2019-07-23 18:05:15 +02:00
parent 672d335160
commit 1ed7dccae4
4 changed files with 30 additions and 13 deletions

View File

@ -7,9 +7,12 @@ import { isRawImage, imageSize, getImageCachePath } from './utils'
export default async function processImage({ driver, addFinishedImage }, id) {
const session = driver.session()
const result = await session.run(`MATCH (p:Photo { id: {id} }) RETURN p`, {
const result = await session.run(
`MATCH (p:Photo { id: {id} })<-[:CONTAINS]-(a:Album) RETURN p, a.id`,
{
id,
})
}
)
await session.run(
`MATCH (p:Photo { id: {id} })-[rel]->(url:PhotoURL) DELETE url, rel`,
@ -17,10 +20,11 @@ export default async function processImage({ driver, addFinishedImage }, id) {
)
const photo = result.records[0].get('p').properties
const albumId = result.records[0].get('a.id')
// console.log('Processing photo', photo.path)
const imagePath = getImageCachePath(id)
const imagePath = getImageCachePath(id, albumId)
await fs.remove(imagePath)
await fs.mkdirp(imagePath)

View File

@ -35,15 +35,15 @@ export default async function scanAlbum(
if (photoResult.records.length != 0) {
// console.log(`Photo already exists ${item}`)
const id = photoResult.records[0].get('p').properties.id
const photoId = photoResult.records[0].get('p').properties.id
const thumbnailPath = path.resolve(
getImageCachePath(id),
getImageCachePath(photoId, id),
'thumbnail.jpg'
)
if (!(await fs.exists(thumbnailPath))) {
processingImagePromises.push(processImage(id))
processingImagePromises.push(processImage(photoId))
} else {
markFinishedImage()
}
@ -86,7 +86,7 @@ export default async function scanAlbum(
)
for (const imageId of deletedImages) {
await fs.remove(getImageCachePath(imageId))
await fs.remove(getImageCachePath(imageId, id))
}
console.log(`Deleted ${deletedImages.length} images from album ${title}`)

View File

@ -1,7 +1,7 @@
import fs from 'fs-extra'
import { resolve as pathResolve } from 'path'
import uuid from 'uuid'
import { isImage } from './utils'
import { isImage, getAlbumCachePath } from './utils'
export default async function scanUser({ driver, scanAlbum }, user) {
console.log('Scanning user', user.username, 'at', user.path)
@ -123,15 +123,25 @@ export default async function scanUser({ driver, scanAlbum }, user) {
await scanPath(user.rootPath)
console.log('Found album ids', foundAlbumIds)
const session = driver.session()
const userAlbumsResult = await session.run(
'MATCH (u:User { id: {userId} })-[:OWNS]->(a:Album)-[:CONTAINS]->(p:Photo) WHERE NOT a.id IN {foundAlbums} DETACH DELETE a, p RETURN a',
`MATCH (u:User { id: {userId} })-[:OWNS]->(a:Album)-[:CONTAINS]->(p:Photo)
WHERE NOT a.id IN {foundAlbums}
WITH a, p, a.id AS albumId
DETACH DELETE a, p
RETURN albumId`,
{ userId: user.id, foundAlbums: foundAlbumIds }
)
const deletedAlbumIds = userAlbumsResult.records.map(record =>
record.get('albumId')
)
for (const albumId of deletedAlbumIds) {
await fs.remove(getAlbumCachePath(albumId))
}
console.log(
`Deleted ${userAlbumsResult.records.length} albums from ${user.username} that was not found locally`
)

View File

@ -34,5 +34,8 @@ export const isRawImage = async path => {
export const imageSize = promisify(require('image-size'))
export const getImageCachePath = id =>
export const getAlbumCachePath = id =>
path.resolve(config.cachePath, 'images', id)
export const getImageCachePath = (imageId, albumId) =>
path.resolve(getAlbumCachePath(albumId), imageId)