mirror of
https://github.com/CaramelFur/Picsur.git
synced 2026-02-25 23:50:47 +01:00
improve image deletion method
This commit is contained in:
@@ -80,7 +80,7 @@ export class ImageDBService {
|
||||
}
|
||||
}
|
||||
|
||||
public async findList(
|
||||
public async delete(
|
||||
ids: string[],
|
||||
userid: string | undefined,
|
||||
): AsyncFailable<EImageBackend[]> {
|
||||
@@ -88,45 +88,34 @@ export class ImageDBService {
|
||||
if (ids.length > 500) return Fail('Too many results');
|
||||
|
||||
try {
|
||||
const found = await this.imageRepo.find({
|
||||
const deletable_images = await this.imageRepo.find({
|
||||
where: {
|
||||
id: In(ids),
|
||||
user_id: userid,
|
||||
},
|
||||
});
|
||||
|
||||
if (found === undefined) return Fail('Images not found');
|
||||
return found;
|
||||
const available_ids = deletable_images.map((i) => i.id);
|
||||
|
||||
if (available_ids.length === 0) return Fail('Images not found');
|
||||
|
||||
await Promise.all([
|
||||
this.imageDerivativeRepo.delete({
|
||||
image_id: In(available_ids),
|
||||
}),
|
||||
this.imageFileRepo.delete({
|
||||
image_id: In(available_ids),
|
||||
}),
|
||||
|
||||
this.imageRepo.delete({ id: In(available_ids) }),
|
||||
]);
|
||||
|
||||
return deletable_images;
|
||||
} catch (e) {
|
||||
return Fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
public async delete(ids: string[]): AsyncFailable<true> {
|
||||
if (ids.length === 0) return true;
|
||||
if (ids.length > 500) return Fail('Too many results');
|
||||
|
||||
try {
|
||||
const derivativesResult = await this.imageDerivativeRepo.delete({
|
||||
image_id: In(ids),
|
||||
});
|
||||
const filesResult = await this.imageFileRepo.delete({
|
||||
image_id: In(ids),
|
||||
});
|
||||
const result = await this.imageRepo.delete({ id: In(ids) });
|
||||
|
||||
if (
|
||||
result.affected === 0 &&
|
||||
filesResult.affected === 0 &&
|
||||
derivativesResult.affected === 0
|
||||
)
|
||||
return Fail('Image not found');
|
||||
} catch (e) {
|
||||
return Fail(e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public async deleteAll(IAmSure: boolean): AsyncFailable<true> {
|
||||
if (!IAmSure)
|
||||
return Fail('You must confirm that you want to delete all images');
|
||||
|
||||
@@ -50,15 +50,7 @@ export class ImageManagerService {
|
||||
ids: string[],
|
||||
userid: string | undefined,
|
||||
): AsyncFailable<EImageBackend[]> {
|
||||
const images = await this.imagesService.findList(ids, userid);
|
||||
if (HasFailed(images)) return images;
|
||||
|
||||
const availableIds = images.map((image) => image.id);
|
||||
|
||||
const deleteResult = await this.imagesService.delete(availableIds);
|
||||
if (HasFailed(deleteResult)) return deleteResult;
|
||||
|
||||
return images;
|
||||
return await this.imagesService.delete(ids, userid);
|
||||
}
|
||||
|
||||
public async upload(
|
||||
|
||||
Reference in New Issue
Block a user