Picsur/frontend/src/app/services/api/image.service.ts

172 lines
4.8 KiB
TypeScript
Raw Normal View History

2022-09-04 10:33:37 +00:00
import { Injectable } from '@angular/core';
2022-05-08 14:12:23 +00:00
import {
ImageDeleteRequest,
ImageDeleteResponse,
ImageListRequest,
ImageListResponse,
2022-09-06 14:32:16 +00:00
ImageUploadResponse,
2022-05-08 14:12:23 +00:00
} from 'picsur-shared/dist/dto/api/image-manage.dto';
2022-06-05 18:38:20 +00:00
import {
ImageMetaResponse,
2022-09-06 14:32:16 +00:00
ImageRequestParams,
2022-06-05 18:38:20 +00:00
} from 'picsur-shared/dist/dto/api/image.dto';
2022-06-27 13:23:06 +00:00
import { ImageLinks } from 'picsur-shared/dist/dto/image-links.class';
2022-08-27 12:24:26 +00:00
import { FileType2Ext } from 'picsur-shared/dist/dto/mimes.dto';
2022-05-08 14:12:23 +00:00
import { EImage } from 'picsur-shared/dist/entities/image.entity';
2022-03-30 10:41:40 +00:00
import { AsyncFailable } from 'picsur-shared/dist/types';
import {
Fail,
FT,
HasFailed,
HasSuccess,
2022-09-06 14:32:16 +00:00
Open,
} from 'picsur-shared/dist/types/failable';
import { UtilService } from 'src/app/util/util.service';
2022-03-28 15:26:50 +00:00
import { ImageUploadRequest } from '../../models/dto/image-upload-request.dto';
2022-03-06 11:34:33 +00:00
import { ApiService } from './api.service';
2022-05-10 09:12:15 +00:00
import { UserService } from './user.service';
2022-02-28 22:18:07 +00:00
@Injectable({
providedIn: 'root',
})
export class ImageService {
2022-05-09 15:02:59 +00:00
constructor(
2022-06-27 15:37:37 +00:00
private readonly api: ApiService,
private readonly util: UtilService,
2022-06-27 15:37:37 +00:00
private readonly userService: UserService,
2022-05-09 15:02:59 +00:00
) {}
2022-02-28 22:18:07 +00:00
public async UploadImage(image: File): AsyncFailable<string> {
const result = await this.api.postForm(
2022-04-25 16:07:36 +00:00
ImageUploadResponse,
2022-05-03 19:37:20 +00:00
'/api/image/upload',
2022-06-05 10:20:16 +00:00
new ImageUploadRequest(image),
2022-02-28 22:18:07 +00:00
);
return Open(result, 'id');
2022-02-28 22:18:07 +00:00
}
2022-04-25 16:07:36 +00:00
public async GetImageMeta(image: string): AsyncFailable<ImageMetaResponse> {
2022-03-12 19:15:48 +00:00
return await this.api.get(ImageMetaResponse, `/i/meta/${image}`);
2022-02-28 22:18:07 +00:00
}
2022-05-10 09:12:15 +00:00
public async ListAllImages(
2022-05-08 14:12:23 +00:00
count: number,
page: number,
2022-06-05 10:20:16 +00:00
userID?: string,
2022-05-08 19:50:53 +00:00
): AsyncFailable<ImageListResponse> {
return await this.api.post(
2022-05-08 14:12:23 +00:00
ImageListRequest,
ImageListResponse,
'/api/image/list',
{
count,
page,
user_id: userID,
2022-06-05 10:20:16 +00:00
},
2022-05-08 14:12:23 +00:00
);
}
2022-05-10 09:12:15 +00:00
public async ListMyImages(
count: number,
2022-06-05 10:20:16 +00:00
page: number,
2022-05-10 09:12:15 +00:00
): AsyncFailable<ImageListResponse> {
const userID = await this.userService.snapshot?.id;
if (userID === undefined) {
2022-07-04 15:11:42 +00:00
return Fail(FT.Authentication, 'User not logged in');
2022-05-10 09:12:15 +00:00
}
return await this.ListAllImages(count, page, userID);
}
2022-05-08 14:12:23 +00:00
public async DeleteImages(
2022-06-05 10:20:16 +00:00
images: string[],
2022-05-08 14:12:23 +00:00
): AsyncFailable<ImageDeleteResponse> {
return await this.api.post(
ImageDeleteRequest,
ImageDeleteResponse,
'/api/image/delete',
{
ids: images,
2022-06-05 10:20:16 +00:00
},
2022-05-08 14:12:23 +00:00
);
}
public async DeleteImage(image: string): AsyncFailable<EImage> {
const result = await this.DeleteImages([image]);
if (HasFailed(result)) return result;
if (result.images.length !== 1) {
return Fail(
2022-07-04 15:11:42 +00:00
FT.Unknown,
2022-06-05 10:20:16 +00:00
`Image ${image} was not deleted, probably lacking permissions`,
2022-05-08 14:12:23 +00:00
);
}
return result.images[0];
}
// Non api calls
2022-08-27 12:24:26 +00:00
public GetImageURL(image: string, filetype: string | null): string {
const baseURL = this.util.getHost();
2022-08-27 12:24:26 +00:00
const extension = FileType2Ext(filetype ?? '');
2022-02-28 22:18:07 +00:00
return `${baseURL}/i/${image}${
HasSuccess(extension) ? '.' + extension : ''
}`;
2022-02-28 22:18:07 +00:00
}
2022-06-05 18:38:20 +00:00
public GetImageURLCustomized(
image: string,
2022-08-27 12:24:26 +00:00
filetype: string | null,
2022-06-05 18:38:20 +00:00
options: ImageRequestParams,
): string {
2022-08-27 12:24:26 +00:00
const baseURL = this.GetImageURL(image, filetype);
2022-06-05 18:38:20 +00:00
const betterOptions = ImageRequestParams.zodSchema.safeParse(options);
if (!betterOptions.success) return baseURL;
let queryParams: string[] = [];
if (options.height !== undefined)
queryParams.push(`height=${options.height}`);
if (options.width !== undefined) queryParams.push(`width=${options.width}`);
if (options.rotate !== undefined)
queryParams.push(`rotate=${options.rotate}`);
if (options.flipx !== undefined) queryParams.push(`flipx=${options.flipx}`);
if (options.flipy !== undefined) queryParams.push(`flipy=${options.flipy}`);
if (options.shrinkonly !== undefined)
queryParams.push(`shrinkonly=${options.shrinkonly}`);
2022-06-05 18:38:20 +00:00
if (options.greyscale !== undefined)
queryParams.push(`greyscale=${options.greyscale}`);
if (options.noalpha !== undefined)
queryParams.push(`noalpha=${options.noalpha}`);
if (options.negative !== undefined)
queryParams.push(`negative=${options.negative}`);
if (options.quality !== undefined)
queryParams.push(`quality=${options.quality}`);
if (queryParams.length === 0) return baseURL;
return baseURL + '?' + queryParams.join('&');
}
2022-04-05 18:37:25 +00:00
public CreateImageLinks(imageURL: string): ImageLinks {
2022-02-28 22:18:07 +00:00
return {
source: imageURL,
markdown: `![image](${imageURL})`,
html: `<img src="${imageURL}" alt="image">`,
rst: `.. image:: ${imageURL}`,
bbcode: `[img]${imageURL}[/img]`,
};
}
2022-04-16 14:35:28 +00:00
2022-04-25 22:02:37 +00:00
public CreateImageLinksFromID(
imageID: string,
2022-06-05 10:20:16 +00:00
mime: string | null,
2022-04-25 22:02:37 +00:00
): ImageLinks {
2022-04-25 16:07:36 +00:00
return this.CreateImageLinks(this.GetImageURL(imageID, mime));
2022-04-16 14:35:28 +00:00
}
2022-02-28 22:18:07 +00:00
}