diff --git a/src/http.ts b/src/http.ts index 31ac0ca..f2e2ef1 100644 --- a/src/http.ts +++ b/src/http.ts @@ -123,12 +123,7 @@ import { SearchResponse } from "./types/SearchResponse"; import { SiteResponse } from "./types/SiteResponse"; import { TransferCommunity } from "./types/TransferCommunity"; import { VerifyEmail } from "./types/VerifyEmail"; -import { - DeleteImage, - UploadImage, - UploadImageResponse, - VERSION, -} from "./other_types"; +import { UploadImage, VERSION } from "./other_types"; import { HideCommunity } from "./types/HideCommunity"; import { GenerateTotpSecretResponse } from "./types/GenerateTotpSecretResponse"; import { UpdateTotp } from "./types/UpdateTotp"; @@ -163,11 +158,14 @@ import { MyUserInfo } from "./types/MyUserInfo"; import { UserBlockInstanceParams } from "./types/UserBlockInstanceParams"; import { AdminAllowInstanceParams } from "./types/AdminAllowInstanceParams"; import { AdminBlockInstanceParams } from "./types/AdminBlockInstanceParams"; +import { DeleteImageParams } from "./types/DeleteImageParams"; +import { UploadImageResponse } from "./types/UploadImageResponse"; enum HttpType { Get = "GET", Post = "POST", Put = "PUT", + Delete = "DELETE", } type RequestOptions = Pick; @@ -1874,6 +1872,8 @@ export class LemmyHttp { /** * Upload an image to the server. + * + * `HTTP.Post /image` */ async uploadImage( { image }: UploadImage, @@ -1881,8 +1881,25 @@ export class LemmyHttp { ): Promise { const formData = createFormData(image); - let url: string | undefined = undefined; - let delete_url: string | undefined = undefined; + const response = await this.#fetchFunction(this.#pictrsUrl, { + ...options, + method: HttpType.Post, + body: formData as unknown as BodyInit, + headers: this.#headers, + }); + return response.json(); + } + + /** + * Upload new user avatar. + * + * `HTTP.Post /account/avatar` + */ + async userUploadAvatar( + { image }: UploadImage, + options?: RequestOptions, + ): Promise { + const formData = createFormData(image); const response = await this.#fetchFunction(this.#pictrsUrl, { ...options, @@ -1890,42 +1907,35 @@ export class LemmyHttp { body: formData as unknown as BodyInit, headers: this.#headers, }); - - if (response.status === 413) { - return { msg: "too_large" }; - } - - const responseJson = await response.json(); - - if (responseJson.msg === "ok") { - const { file: hash, delete_token: deleteToken } = responseJson.files[0]; - delete_url = `${this.#pictrsUrl}/delete/${deleteToken}/${hash}`; - url = `${this.#pictrsUrl}/${hash}`; - } - - return { - ...responseJson, - url, - delete_url, - }; + return response.json(); } /** * Delete a pictrs image + * + * `HTTP.Delete /image` */ - async deleteImage( - { token, filename }: DeleteImage, - options?: RequestOptions, - ): Promise { - const deleteUrl = `${this.#pictrsUrl}/delete/${token}/${filename}`; + async deleteImage(form: DeleteImageParams, options?: RequestOptions) { + return this.#wrapper( + HttpType.Delete, + "/image", + form, + options, + ); + } - const response = await this.#fetchFunction(deleteUrl, { - ...options, - method: HttpType.Get, - headers: this.#headers, - }); - - return response.status == 204; + /** + * Health check for image functionality + * + * `HTTP.Get /image/health` + */ + async imageHealth(options?: RequestOptions) { + return this.#wrapper( + HttpType.Get, + "/image/health", + {}, + options, + ); } #buildFullUrl(endpoint: string) { diff --git a/src/index.ts b/src/index.ts index eb60cf4..906bb0f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -85,6 +85,7 @@ export { DeleteAccount } from "./types/DeleteAccount"; export { DeleteComment } from "./types/DeleteComment"; export { DeleteCommunity } from "./types/DeleteCommunity"; export { DeleteCustomEmoji } from "./types/DeleteCustomEmoji"; +export { DeleteImageParams } from "./types/DeleteImageParams"; export { DeleteOAuthProvider } from "./types/DeleteOAuthProvider"; export { DeletePost } from "./types/DeletePost"; export { DeletePrivateMessage } from "./types/DeletePrivateMessage"; @@ -138,6 +139,8 @@ export { GetUnreadRegistrationApplicationCountResponse } from "./types/GetUnread export { HideCommunity } from "./types/HideCommunity"; export { HidePost } from "./types/HidePost"; export { ImageDetails } from "./types/ImageDetails"; +export { ImageGetParams } from "./types/ImageGetParams"; +export { ImageProxyParams } from "./types/ImageProxyParams"; export { Instance } from "./types/Instance"; export { InstanceId } from "./types/InstanceId"; export { InstanceWithFederationState } from "./types/InstanceWithFederationState"; @@ -291,6 +294,7 @@ export { TransferCommunity } from "./types/TransferCommunity"; export { UpdateTagline } from "./types/UpdateTagline"; export { UpdateTotp } from "./types/UpdateTotp"; export { UpdateTotpResponse } from "./types/UpdateTotpResponse"; +export { UploadImageResponse } from "./types/UploadImageResponse"; export { UserBlockInstanceParams } from "./types/UserBlockInstanceParams"; export { VerifyEmail } from "./types/VerifyEmail"; export { VoteView } from "./types/VoteView"; diff --git a/src/other_types.ts b/src/other_types.ts index 27e2e33..b3be06c 100644 --- a/src/other_types.ts +++ b/src/other_types.ts @@ -3,23 +3,3 @@ export const VERSION = "v4"; export interface UploadImage { image: File | Buffer; } - -export interface UploadImageResponse { - /** - * Is "ok" if the upload was successful; is something else otherwise. - */ - msg: string; - files?: ImageFile[]; - url?: string; - delete_url?: string; -} - -export interface ImageFile { - file: string; - delete_token: string; -} - -export interface DeleteImage { - token: string; - filename: string; -} diff --git a/src/types/DeleteImageParams.ts b/src/types/DeleteImageParams.ts new file mode 100644 index 0000000..c0df70e --- /dev/null +++ b/src/types/DeleteImageParams.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type DeleteImageParams = { file: string; token: string }; diff --git a/src/types/ImageGetParams.ts b/src/types/ImageGetParams.ts new file mode 100644 index 0000000..c2dbfab --- /dev/null +++ b/src/types/ImageGetParams.ts @@ -0,0 +1,6 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type ImageGetParams = { + format: string | null; + thumbnail: number | null; +}; diff --git a/src/types/ImageProxyParams.ts b/src/types/ImageProxyParams.ts new file mode 100644 index 0000000..1552bbc --- /dev/null +++ b/src/types/ImageProxyParams.ts @@ -0,0 +1,7 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type ImageProxyParams = { + url: string; + format: string | null; + thumbnail: number | null; +}; diff --git a/src/types/LemmyErrorType.ts b/src/types/LemmyErrorType.ts index 32b9c9e..01af7e9 100644 --- a/src/types/LemmyErrorType.ts +++ b/src/types/LemmyErrorType.ts @@ -22,6 +22,7 @@ export type LemmyErrorType = | { error: "pictrs_api_key_not_provided" } | { error: "no_content_type_header" } | { error: "not_an_image_type" } + | { error: "invalid_image_upload" } | { error: "not_a_mod_or_admin" } | { error: "not_top_mod" } | { error: "not_logged_in" } diff --git a/src/types/SaveUserSettings.ts b/src/types/SaveUserSettings.ts index 431a8dd..f6439a1 100644 --- a/src/types/SaveUserSettings.ts +++ b/src/types/SaveUserSettings.ts @@ -42,10 +42,6 @@ export type SaveUserSettings = { * The language of the lemmy interface */ interface_language?: string; - /** - * A URL for your avatar. - */ - avatar?: string; /** * A URL for your banner. */ diff --git a/src/types/UploadImageResponse.ts b/src/types/UploadImageResponse.ts new file mode 100644 index 0000000..afc7d98 --- /dev/null +++ b/src/types/UploadImageResponse.ts @@ -0,0 +1,7 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type UploadImageResponse = { + image_url: string; + filename: string; + delete_token: string; +};