From 297582fc65efa1db31705e49ce9bcf9ca3b9971e Mon Sep 17 00:00:00 2001 From: paco Date: Wed, 23 Feb 2022 13:17:55 +0100 Subject: [PATCH] edit many paths --- api/src/graphql/images.sdl.ts | 9 +- api/src/services/images/images.test.ts | 156 ++++++++++++++++++------- api/src/services/images/images.ts | 50 ++++---- 3 files changed, 150 insertions(+), 65 deletions(-) diff --git a/api/src/graphql/images.sdl.ts b/api/src/graphql/images.sdl.ts index 9cf3350..f929cb2 100644 --- a/api/src/graphql/images.sdl.ts +++ b/api/src/graphql/images.sdl.ts @@ -27,6 +27,9 @@ export const schema = gql` image(id: String!): Image @requireAuth } + type EditManyResult { + count: Int! + } type DeleteManyResult { count: Int! } @@ -35,8 +38,10 @@ export const schema = gql` deleteManyImages(imageIds: [String!]!): DeleteManyResult! @requireAuth deleteManyImagesWithFilter(filter: ImageFilters!): DeleteManyResult! @requireAuth - editImageBasePath(imageId: String!, basePath: String!): Boolean! - @requireAuth + editImagesBasePath( + imageIds: [String!]! + basePath: String! + ): EditManyResult! @requireAuth } input ImageFilters { diff --git a/api/src/services/images/images.test.ts b/api/src/services/images/images.test.ts index 808f4e2..6f0b516 100644 --- a/api/src/services/images/images.test.ts +++ b/api/src/services/images/images.test.ts @@ -1,7 +1,7 @@ import { deleteManyImages, deleteManyImagesWithFilter, - editImageBasePath, + editImagesBasePath, image, images, moreImages, @@ -279,7 +279,9 @@ describe('images', () => { const imagesToDelete = [scenario.image.p1, scenario.image.p3] const result = await deleteManyImages({ - imageIds: imagesToDelete.map((i) => i.id), + imageIds: ['not-existing'] + .concat(imagesToDelete.map((i) => i.id)) + .concat('neither'), }) expect(result.count).toEqual(imagesToDelete.length) @@ -343,53 +345,123 @@ describe('images', () => { ) }) describe('edit', () => { - async function testEditImageBasePath(imageToEdit, basePath, newPath) { - await Buckets.photos.put(imageToEdit.path, imageToEdit.path) - await Buckets.miniatures.put(imageToEdit.path, imageToEdit.path) + async function testEditImagesBasePath( + imagesToEdit, + basePath, + newPaths, + count + ) { + for (const i in imagesToEdit) { + const imageToEdit = imagesToEdit[i] + if (!imageToEdit) continue + await Buckets.photos.put(imageToEdit.path, imageToEdit.path) + await Buckets.miniatures.put(imageToEdit.path, imageToEdit.path) + } - const result = await editImageBasePath({ - imageId: imageToEdit.id, + const result = await editImagesBasePath({ + imageIds: imagesToEdit.map((i) => (i ? i.id : 'numbid')), basePath, }) - expect(result).toBe(true) + expect(result.count).toBe(count) - const resImage = await image({ id: imageToEdit.id }) - expect(resImage.path).toEqual(newPath) + for (const i in imagesToEdit) { + const imageToEdit = imagesToEdit[i] + if (!imageToEdit) continue + const newPath = newPaths[i] + const resImage = await image({ id: imageToEdit.id }) + expect(resImage.path).toEqual(newPath) - expect((await Buckets.photos.get(newPath)).toString()).toEqual( - imageToEdit.path - ) - expect((await Buckets.miniatures.get(newPath)).toString()).toEqual( - imageToEdit.path - ) - if (imageToEdit.path !== newPath) { - expect(await Buckets.photos.get(imageToEdit.path)).toBeNull() - expect(await Buckets.miniatures.get(imageToEdit.path)).toBeNull() + expect((await Buckets.photos.get(newPath)).toString()).toEqual( + imageToEdit.path + ) + expect((await Buckets.miniatures.get(newPath)).toString()).toEqual( + imageToEdit.path + ) + if (imageToEdit.path !== newPath) { + expect(await Buckets.photos.get(imageToEdit.path)).toBeNull() + expect(await Buckets.miniatures.get(imageToEdit.path)).toBeNull() + } } } - scenario('editImageBasePath 1', async (scenario: StandardScenario) => { - await testEditImageBasePath( - scenario.image.p3, - 'patate/labas', - 'patate/labas/p3.jpg' - ) - }) - scenario('editImageBasePath 2', async (scenario: StandardScenario) => { - await testEditImageBasePath( - scenario.image.p5, - 'caca/caca', - 'caca/caca/p5.jpg' - ) - }) - scenario('editImageBasePath 3', async (scenario: StandardScenario) => { - await testEditImageBasePath(scenario.image.p5, '', 'p5.jpg') - }) - scenario('editImageBasePath 4', async (scenario: StandardScenario) => { - await testEditImageBasePath(scenario.image.p4, 'ath3', 'ath3/p4.jpg') - }) - scenario('editImageBasePath 5', async (scenario: StandardScenario) => { - await testEditImageBasePath(scenario.image.p4, '', 'p4.jpg') - }) + scenario( + 'editImagesBasePath image with path', + async (scenario: StandardScenario) => { + await testEditImagesBasePath( + [scenario.image.p3], + 'patate/labas', + ['patate/labas/p3.jpg'], + 1 + ) + } + ) + scenario( + 'editImagesBasePath image without path', + async (scenario: StandardScenario) => { + await testEditImagesBasePath( + [scenario.image.p5], + 'caca/caca', + ['caca/caca/p5.jpg'], + 1 + ) + } + ) + + scenario( + 'editImagesBasePath image mixed path', + async (scenario: StandardScenario) => { + await testEditImagesBasePath( + [scenario.image.p3, scenario.image.p5], + 'ici/labas', + ['ici/labas/p3.jpg', 'ici/labas/p5.jpg'], + 2 + ) + } + ) + scenario( + 'editImagesBasePath image one same path', + async (scenario: StandardScenario) => { + await testEditImagesBasePath( + [scenario.image.p4, scenario.image.p5], + 'ath3', + ['ath3/p4.jpg', 'ath3/p5.jpg'], + 1 + ) + } + ) + scenario( + 'editImagesBasePath keep same path nopath', + async (scenario: StandardScenario) => { + await testEditImagesBasePath([scenario.image.p5], '', ['p5.jpg'], 0) + } + ) + scenario( + 'editImagesBasePath keep same path with path', + async (scenario: StandardScenario) => { + await testEditImagesBasePath( + [scenario.image.p4], + 'ath3', + ['ath3/p4.jpg'], + 0 + ) + } + ) + scenario( + 'editImagesBasePath remove basepath', + async (scenario: StandardScenario) => { + await testEditImagesBasePath([scenario.image.p4], '', ['p4.jpg'], 1) + } + ) + scenario( + 'editImagesBasePath with not existing id', + async (scenario: StandardScenario) => { + await testEditImagesBasePath( + [scenario.image.p4, null], + 'papapapa/papap', + ['papapapa/papap/p4.jpg'], + 1 + ) + } + ) }) }) diff --git a/api/src/services/images/images.ts b/api/src/services/images/images.ts index 1e66130..b01210a 100644 --- a/api/src/services/images/images.ts +++ b/api/src/services/images/images.ts @@ -4,7 +4,7 @@ import type { QueryimagesArgs, MutationdeleteManyImagesArgs, MutationdeleteManyImagesWithFilterArgs, - MutationeditImageBasePathArgs, + MutationeditImagesBasePathArgs, Mutation, } from 'types/graphql' @@ -113,7 +113,7 @@ export const deleteManyImages = async ({ id: imageId, }, }) - if (!image) break + if (!image) continue await Buckets.photos.delete(image.path) await Buckets.miniatures.delete(image.path) await db.image.delete({ @@ -141,25 +141,33 @@ export const deleteManyImagesWithFilter = async ({ return deleteManyImages({ imageIds: imagesToApply.map((i) => i.id) }) } -export const editImageBasePath = async ({ - imageId, +export const editImagesBasePath = async ({ + imageIds, basePath, -}: MutationeditImageBasePathArgs): Promise => { - const imageToEdit = await image({ id: imageId }) - if (!imageToEdit) { - throw new Error('NOT_FOUND') +}: MutationeditImagesBasePathArgs): Promise => { + let count = 0 + for (const i in imageIds) { + const imageId = imageIds[i] + const imageToEdit = await image({ id: imageId }) + if (!imageToEdit) { + continue + } + const fileName = getFileName(imageToEdit.path) + const path = getPath(basePath, fileName) + if (path === imageToEdit.path) continue + + await db.image.update({ + where: { id: imageId }, + data: { + path, + }, + }) + await Buckets.photos.editKey(imageToEdit.path, path) + await Buckets.miniatures.editKey(imageToEdit.path, path) + + count++ + } + return { + count, } - const fileName = getFileName(imageToEdit.path) - const path = getPath(basePath, fileName) - if (path === imageToEdit.path) return true - - await db.image.update({ - where: { id: imageId }, - data: { - path, - }, - }) - await Buckets.photos.editKey(imageToEdit.path, path) - await Buckets.miniatures.editKey(imageToEdit.path, path) - return true }