🌟basic status, while uploading directories (#216)
* 🌟 basic status, while uploading directories * 🐛 fix the infinite redirection when the token always expired
This commit is contained in:
@@ -10,7 +10,6 @@ import Languages from 'features/global/services/languages-service';
|
||||
|
||||
/**
|
||||
* Returns the children of a drive item
|
||||
* @param id
|
||||
* @returns
|
||||
*/
|
||||
export const useDriveActions = () => {
|
||||
@@ -47,11 +46,12 @@ export const useDriveActions = () => {
|
||||
|
||||
const create = useCallback(
|
||||
async (item: Partial<DriveItem>, version: Partial<DriveItemVersion>) => {
|
||||
if (!item || !version) throw new Error("All ");
|
||||
let driveFile = null;
|
||||
if (!item.company_id) item.company_id = companyId;
|
||||
try {
|
||||
driveFile = await DriveApiClient.create(companyId, { item, version });
|
||||
await refresh(item.parent_id!);
|
||||
await refresh(driveFile.parent_id!);
|
||||
} catch (e) {
|
||||
ToasterService.error(Languages.t('hooks.use-drive-actions.unable_create_file'));
|
||||
}
|
||||
@@ -63,7 +63,7 @@ export const useDriveActions = () => {
|
||||
const download = useCallback(
|
||||
async (id: string, versionId?: string) => {
|
||||
try {
|
||||
const url = await DriveApiClient.getDownloadUrl(companyId, id, versionId);
|
||||
const url = DriveApiClient.getDownloadUrl(companyId, id, versionId);
|
||||
(window as any).open(url, '_blank').focus();
|
||||
} catch (e) {
|
||||
ToasterService.error(Languages.t('hooks.use-drive-actions.unable_download_file'));
|
||||
|
||||
@@ -3,14 +3,16 @@ import FileUploadService from '@features/files/services/file-upload-service';
|
||||
import { ToasterService } from '@features/global/services/toaster-service';
|
||||
import { DriveApiClient } from '../api-client/api-client';
|
||||
import { useDriveActions } from './use-drive-actions';
|
||||
import Logger from '@features/global/framework/logger-service';
|
||||
|
||||
/**
|
||||
* Returns the children of a drive item
|
||||
* @param id
|
||||
* @returns
|
||||
*/
|
||||
export const useDriveUpload = () => {
|
||||
const { create } = useDriveActions();
|
||||
const { create, refresh } = useDriveActions();
|
||||
|
||||
const logger = Logger.getLogger('useDriveUpload')
|
||||
|
||||
const uploadVersion = async (file: File, context: { companyId: string; id: string }) => {
|
||||
return new Promise(r => {
|
||||
@@ -45,47 +47,23 @@ export const useDriveUpload = () => {
|
||||
tree: FileTreeObject,
|
||||
context: { companyId: string; parentId: string },
|
||||
) => {
|
||||
const filesPerParentId: { [key: string]: File[] } = {};
|
||||
|
||||
// Create all directories
|
||||
console.debug("Start creating directories ...");
|
||||
const createDirectories = async (tree: FileTreeObject['tree'], parentId: string) => {
|
||||
for (const directory of Object.keys(tree)) {
|
||||
if (tree[directory] instanceof File) {
|
||||
if (!filesPerParentId[parentId]) filesPerParentId[parentId] = [];
|
||||
filesPerParentId[parentId].push(tree[directory] as File);
|
||||
} else {
|
||||
console.debug(`Create directory ${directory}`);
|
||||
const driveItem = await create(
|
||||
{
|
||||
company_id: context.companyId,
|
||||
parent_id: parentId,
|
||||
name: directory,
|
||||
is_directory: true,
|
||||
},
|
||||
{},
|
||||
);
|
||||
console.debug(`Directory ${directory} created`);
|
||||
if (driveItem?.id) {
|
||||
await createDirectories(tree[directory] as FileTreeObject['tree'], driveItem.id);
|
||||
} else {
|
||||
throw new Error('Could not create directory');
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
await createDirectories(tree.tree, context.parentId);
|
||||
console.debug("All directories created");
|
||||
logger.debug("Start creating directories ...");
|
||||
const filesPerParentId = await FileUploadService.createDirectories(tree.tree, context);
|
||||
await refresh(context.parentId);
|
||||
logger.debug("All directories created");
|
||||
|
||||
// Upload files into directories
|
||||
logger.debug("Start file uploading")
|
||||
for (const parentId of Object.keys(filesPerParentId)) {
|
||||
logger.debug(`Upload files for directory ${parentId}`);
|
||||
await FileUploadService.upload(filesPerParentId[parentId], {
|
||||
context: {
|
||||
companyId: context.companyId,
|
||||
parentId: parentId,
|
||||
},
|
||||
callback: (file, context) => {
|
||||
console.log('created file: ', file);
|
||||
logger.debug('created file: ', file);
|
||||
if (file) {
|
||||
create(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user