Skip to content

Commit

Permalink
feat: allow to download multiple files with long pulling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Nov 14, 2023
1 parent ee056b4 commit 558c791
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
2 changes: 1 addition & 1 deletion filesmanager/static/html/bundle.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions react-app/components/FileManager/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ export const useFileActionHandler = (
moveFiles,
createFolder,
addFile,
downloadFile
downloadFile,
downloadFiles,
) => {
return useCallback(
(data) => {
Expand Down Expand Up @@ -254,7 +255,7 @@ export const useFileActionHandler = (
}

if (selectedFiles.length > 1) {
alert('You must select a one file to download');
downloadFiles(selectedFiles);
}
}
},
Expand Down
40 changes: 38 additions & 2 deletions react-app/components/FileManager/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import xBlockContext from '@constants/xBlockContext';
import useXBlockActionButtons from '@hooks/useXBlockActionButtons';
import useFileDownloader from '@hooks/useFileDownloader';
import useAddErrorMessageToModal from '@hooks/useAddErrorMessageToModal';
import { syncContent, deleteContent } from '@services/directoriesService';
import { syncContent, deleteContent, downloadContent, downloadStatus } from '@services/directoriesService';
import ErrorMessage from '@components/ErrorMessage';

import { useCustomFileMap, useFiles, useFolderChain, useFileActionHandler } from './hooks';
Expand Down Expand Up @@ -58,6 +58,41 @@ const FileManager = (props) => {
downloadFileHook(fullUrl, display_name);
};

const downloadFiles = (filesToDownload) => {
downloadFilesTemp(filesToDownload)
};

const downloadFilesTemp = async (filesToDownload) => {
try {
const createContentData = await downloadContent({ contents: filesToDownload });
if (createContentData.status !== StatusCodes.OK) {
throw new Error('Download content has failed: Unexpected status code');
}
let data = createContentData.data;
getStatus(data.task_id)

return Promise.resolve('Downloading content was successfully');
} catch (error) {
return Promise.reject('An error has ocurred while we tried to download assets');
}
};

const getStatus = async (taskID) => {
const createContentData = await downloadStatus(taskID);
if (createContentData.status !== StatusCodes.OK) {
throw new Error('Fetching task status has failed: Unexpected status code');
}
let data = createContentData.data;
if (data.status === 'SUCCESS') {
downloadFileHook(data.result, "download.zip")
} else {
setTimeout(() => {
getStatus(taskID)
}, 1000)
}
return Promise.resolve('Fetching download was successfully');
}

const {
fileMap,
currentFolderId,
Expand Down Expand Up @@ -94,7 +129,8 @@ const FileManager = (props) => {
moveFiles,
createFolder,
addFile,
downloadFile
downloadFile,
downloadFiles
);

const { xblockId, isEditView } = xBlockContext;
Expand Down
12 changes: 12 additions & 0 deletions react-app/services/directoriesService.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@ export const deleteContent = (assetKeysToDelete) => {
const createContentHandler = xBlockContext.runtime.handlerUrl(globalElement, 'delete_content');
return apiConfig.post(createContentHandler, assetKeysToDelete);
}

export const downloadContent = (assetKeysToDownload) => {
const { element: globalElement } = xBlockContext;
const handler = xBlockContext.runtime.handlerUrl(globalElement, 'download_content');
return apiConfig.post(handler, assetKeysToDownload);
}

export const downloadStatus = (taskID) => {
const { element: globalElement } = xBlockContext;
const handler = xBlockContext.runtime.handlerUrl(globalElement, 'download_status');
return apiConfig.post(handler, { task_id: taskID });
}

0 comments on commit 558c791

Please sign in to comment.