2024-04-29 06:42:33 +00:00
|
|
|
import axios from "axios";
|
2023-11-28 06:04:20 +00:00
|
|
|
// helpers
|
2024-03-19 14:38:35 +00:00
|
|
|
import { API_BASE_URL } from "@/helpers/common.helper";
|
2024-04-29 06:42:33 +00:00
|
|
|
// services
|
|
|
|
import APIService from "@/services/api.service";
|
2023-09-01 11:12:30 +00:00
|
|
|
|
|
|
|
interface UnSplashImage {
|
|
|
|
id: string;
|
|
|
|
created_at: Date;
|
|
|
|
updated_at: Date;
|
|
|
|
promoted_at: Date;
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
color: string;
|
|
|
|
blur_hash: string;
|
|
|
|
description: null;
|
|
|
|
alt_description: string;
|
|
|
|
urls: UnSplashImageUrls;
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface UnSplashImageUrls {
|
|
|
|
raw: string;
|
|
|
|
full: string;
|
|
|
|
regular: string;
|
|
|
|
small: string;
|
|
|
|
thumb: string;
|
|
|
|
small_s3: string;
|
|
|
|
}
|
|
|
|
|
2023-10-13 06:35:49 +00:00
|
|
|
class FileService extends APIService {
|
2023-11-08 12:30:53 +00:00
|
|
|
private cancelSource: any;
|
|
|
|
|
2023-09-01 11:12:30 +00:00
|
|
|
constructor() {
|
2023-09-13 14:51:02 +00:00
|
|
|
super(API_BASE_URL);
|
2023-10-13 06:35:49 +00:00
|
|
|
this.uploadFile = this.uploadFile.bind(this);
|
|
|
|
this.deleteImage = this.deleteImage.bind(this);
|
2023-11-28 06:04:20 +00:00
|
|
|
this.restoreImage = this.restoreImage.bind(this);
|
2023-11-08 12:30:53 +00:00
|
|
|
this.cancelUpload = this.cancelUpload.bind(this);
|
2023-09-01 11:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async uploadFile(workspaceSlug: string, file: FormData): Promise<any> {
|
2023-11-08 12:30:53 +00:00
|
|
|
this.cancelSource = axios.CancelToken.source();
|
2023-10-13 06:35:49 +00:00
|
|
|
return this.post(`/api/workspaces/${workspaceSlug}/file-assets/`, file, {
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "multipart/form-data",
|
|
|
|
},
|
2023-11-08 12:30:53 +00:00
|
|
|
cancelToken: this.cancelSource.token,
|
2023-10-13 06:35:49 +00:00
|
|
|
})
|
2023-09-01 11:12:30 +00:00
|
|
|
.then((response) => response?.data)
|
|
|
|
.catch((error) => {
|
2023-11-08 12:30:53 +00:00
|
|
|
if (axios.isCancel(error)) {
|
|
|
|
console.log(error.message);
|
|
|
|
} else {
|
2023-11-28 06:04:20 +00:00
|
|
|
console.log(error);
|
2023-11-08 12:30:53 +00:00
|
|
|
throw error?.response?.data;
|
|
|
|
}
|
2023-09-01 11:12:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-08 12:30:53 +00:00
|
|
|
cancelUpload() {
|
|
|
|
this.cancelSource.cancel("Upload cancelled");
|
|
|
|
}
|
2023-11-28 06:04:20 +00:00
|
|
|
|
2023-10-13 06:35:49 +00:00
|
|
|
getUploadFileFunction(workspaceSlug: string): (file: File) => Promise<string> {
|
|
|
|
return async (file: File) => {
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("asset", file);
|
|
|
|
formData.append("attributes", JSON.stringify({}));
|
|
|
|
|
|
|
|
const data = await this.uploadFile(workspaceSlug, formData);
|
|
|
|
return data.asset;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-24 13:26:19 +00:00
|
|
|
getDeleteImageFunction(workspaceId: string) {
|
|
|
|
return async (src: string) => {
|
|
|
|
try {
|
|
|
|
const assetUrlWithWorkspaceId = `${workspaceId}/${this.extractAssetIdFromUrl(src, workspaceId)}`;
|
|
|
|
const data = await this.deleteImage(assetUrlWithWorkspaceId);
|
|
|
|
return data;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getRestoreImageFunction(workspaceId: string) {
|
|
|
|
return async (src: string) => {
|
|
|
|
try {
|
|
|
|
const assetUrlWithWorkspaceId = `${workspaceId}/${this.extractAssetIdFromUrl(src, workspaceId)}`;
|
|
|
|
const data = await this.restoreImage(assetUrlWithWorkspaceId);
|
|
|
|
return data;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
extractAssetIdFromUrl(src: string, workspaceId: string): string {
|
|
|
|
const indexWhereAssetIdStarts = src.indexOf(workspaceId) + workspaceId.length + 1;
|
|
|
|
if (indexWhereAssetIdStarts === -1) {
|
|
|
|
throw new Error("Workspace ID not found in source string");
|
|
|
|
}
|
|
|
|
const assetUrl = src.substring(indexWhereAssetIdStarts);
|
|
|
|
return assetUrl;
|
|
|
|
}
|
|
|
|
|
2023-09-01 11:12:30 +00:00
|
|
|
async deleteImage(assetUrlWithWorkspaceId: string): Promise<any> {
|
|
|
|
return this.delete(`/api/workspaces/file-assets/${assetUrlWithWorkspaceId}/`)
|
|
|
|
.then((response) => response?.status)
|
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-28 06:04:20 +00:00
|
|
|
async restoreImage(assetUrlWithWorkspaceId: string): Promise<any> {
|
|
|
|
return this.post(`/api/workspaces/file-assets/${assetUrlWithWorkspaceId}/restore/`, {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
})
|
|
|
|
.then((response) => response?.status)
|
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-01 11:12:30 +00:00
|
|
|
async deleteFile(workspaceId: string, assetUrl: string): Promise<any> {
|
|
|
|
const lastIndex = assetUrl.lastIndexOf("/");
|
|
|
|
const assetId = assetUrl.substring(lastIndex + 1);
|
|
|
|
|
|
|
|
return this.delete(`/api/workspaces/file-assets/${workspaceId}/${assetId}/`)
|
|
|
|
.then((response) => response?.data)
|
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
2024-04-29 06:42:33 +00:00
|
|
|
|
2023-09-01 11:12:30 +00:00
|
|
|
async uploadUserFile(file: FormData): Promise<any> {
|
2024-04-29 06:42:33 +00:00
|
|
|
return this.post(`/api/users/file-assets/`, file, {
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "multipart/form-data",
|
|
|
|
},
|
|
|
|
})
|
2023-09-01 11:12:30 +00:00
|
|
|
.then((response) => response?.data)
|
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteUserFile(assetUrl: string): Promise<any> {
|
|
|
|
const lastIndex = assetUrl.lastIndexOf("/");
|
|
|
|
const assetId = assetUrl.substring(lastIndex + 1);
|
|
|
|
|
|
|
|
return this.delete(`/api/users/file-assets/${assetId}`)
|
|
|
|
.then((response) => response?.data)
|
|
|
|
.catch((error) => {
|
|
|
|
throw error?.response?.data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 06:35:49 +00:00
|
|
|
const fileService = new FileService();
|
2023-09-01 11:12:30 +00:00
|
|
|
|
2023-10-13 06:35:49 +00:00
|
|
|
export default fileService;
|