mirror of
https://github.com/stonith404/pingvin-share.git
synced 2025-01-15 01:14:27 -05:00
653d72bcb9
* add first concept * finished first concept * allow 3 uploads at same time * retry if chunk failed * updated clean temporary files job * fix throttling for chunk uploads * update tests * remove multer * migrate from `MAX_FILE_SIZE` to `MAX_SHARE_SIZE` * improve error handling if file failed to upload * fix promise limit * improve file progress
112 lines
2.6 KiB
TypeScript
112 lines
2.6 KiB
TypeScript
import { FileUploadResponse } from "../types/File.type";
|
|
import {
|
|
CreateShare,
|
|
MyShare,
|
|
Share,
|
|
ShareMetaData,
|
|
} from "../types/share.type";
|
|
import api from "./api.service";
|
|
|
|
const create = async (share: CreateShare) => {
|
|
const { id, expiration, recipients, security, description } = share;
|
|
return (
|
|
await api.post("shares", {
|
|
id,
|
|
expiration,
|
|
recipients,
|
|
security,
|
|
description,
|
|
})
|
|
).data;
|
|
};
|
|
|
|
const completeShare = async (id: string) => {
|
|
return (await api.post(`shares/${id}/complete`)).data;
|
|
};
|
|
|
|
const get = async (id: string): Promise<Share> => {
|
|
const shareToken = sessionStorage.getItem(`share_${id}_token`);
|
|
return (
|
|
await api.get(`shares/${id}`, {
|
|
headers: { "X-Share-Token": shareToken ?? "" },
|
|
})
|
|
).data;
|
|
};
|
|
|
|
const getMetaData = async (id: string): Promise<ShareMetaData> => {
|
|
const shareToken = sessionStorage.getItem(`share_${id}_token`);
|
|
return (
|
|
await api.get(`shares/${id}/metaData`, {
|
|
headers: { "X-Share-Token": shareToken ?? "" },
|
|
})
|
|
).data;
|
|
};
|
|
|
|
const remove = async (id: string) => {
|
|
await api.delete(`shares/${id}`);
|
|
};
|
|
|
|
const getMyShares = async (): Promise<MyShare[]> => {
|
|
return (await api.get("shares")).data;
|
|
};
|
|
|
|
const getShareToken = async (id: string, password?: string) => {
|
|
const { token } = (await api.post(`/shares/${id}/token`, { password })).data;
|
|
|
|
sessionStorage.setItem(`share_${id}_token`, token);
|
|
};
|
|
|
|
const isShareIdAvailable = async (id: string): Promise<boolean> => {
|
|
return (await api.get(`shares/isShareIdAvailable/${id}`)).data.isAvailable;
|
|
};
|
|
|
|
const getFileDownloadUrl = async (shareId: string, fileId: string) => {
|
|
const shareToken = sessionStorage.getItem(`share_${shareId}_token`);
|
|
return (
|
|
await api.get(`shares/${shareId}/files/${fileId}/download`, {
|
|
headers: { "X-Share-Token": shareToken ?? "" },
|
|
})
|
|
).data.url;
|
|
};
|
|
|
|
const downloadFile = async (shareId: string, fileId: string) => {
|
|
window.location.href = await getFileDownloadUrl(shareId, fileId);
|
|
};
|
|
|
|
const uploadFile = async (
|
|
shareId: string,
|
|
readerEvent: ProgressEvent<FileReader>,
|
|
file: {
|
|
id?: string;
|
|
name: string;
|
|
},
|
|
chunkIndex: number,
|
|
totalChunks: number
|
|
): Promise<FileUploadResponse> => {
|
|
const data = readerEvent.target!.result;
|
|
|
|
return (
|
|
await api.post(`shares/${shareId}/files`, data, {
|
|
headers: { "Content-Type": "application/octet-stream" },
|
|
params: {
|
|
id: file.id,
|
|
name: file.name,
|
|
chunkIndex,
|
|
totalChunks,
|
|
},
|
|
})
|
|
).data;
|
|
};
|
|
|
|
export default {
|
|
create,
|
|
completeShare,
|
|
getShareToken,
|
|
get,
|
|
remove,
|
|
getMetaData,
|
|
getMyShares,
|
|
isShareIdAvailable,
|
|
downloadFile,
|
|
uploadFile,
|
|
};
|