diff --git a/api/src/scanner/processImage.js b/api/src/scanner/processImage.js index ab80f34e..6f362bef 100644 --- a/api/src/scanner/processImage.js +++ b/api/src/scanner/processImage.js @@ -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`, { - id, - }) + 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) diff --git a/api/src/scanner/scanAlbum.js b/api/src/scanner/scanAlbum.js index b956532d..17ef1480 100644 --- a/api/src/scanner/scanAlbum.js +++ b/api/src/scanner/scanAlbum.js @@ -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}`) diff --git a/api/src/scanner/scanUser.js b/api/src/scanner/scanUser.js index 150b97ad..07fde7fc 100644 --- a/api/src/scanner/scanUser.js +++ b/api/src/scanner/scanUser.js @@ -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` ) diff --git a/api/src/scanner/utils.js b/api/src/scanner/utils.js index 101b9474..f9733e9e 100644 --- a/api/src/scanner/utils.js +++ b/api/src/scanner/utils.js @@ -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)