Skip to content

Commit

Permalink
fix: fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sherry committed Sep 26, 2023
1 parent fb19202 commit ac03a17
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 38 deletions.
49 changes: 26 additions & 23 deletions src/components/Dropdowns/S3BucketFileTableDropdown.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
/* eslint-disable jsx-a11y/anchor-is-valid */
import React, { useEffect } from 'react';
import React, { useEffect, useState } from 'react';
import { createPopper } from '@popperjs/core';
import Emitter from 'utils/eventBus';
import { t } from 'utils/text.js';
import { downloadFile } from 'services/s3Service';
// import { downloadFile } from 'services/s3Service';
import { downloadFile } from 'services/filesService.js';
import * as AWS from "@aws-sdk/client-s3";


const { DeleteObjectsCommand, GetObjectCommand, CopyObjectCommand, ListObjectsCommand } = AWS;
const { DeleteObjectsCommand, CopyObjectCommand, ListObjectsCommand } = AWS;


const S3BucketFileTableDropdown = ({ color, item, globalS3, bucketName }) => {
const [dropdownPopoverShow, setDropdownPopoverShow] = React.useState(false);
const btnDropdownRef = React.createRef();
const popoverDropdownRef = React.createRef();
// eslint-disable-next-line no-unused-vars
const [err, setErr] = useState(false);

const openDropdownPopover = () => {
createPopper(btnDropdownRef.current, popoverDropdownRef.current, {
placement: 'left-start',
Expand All @@ -34,46 +38,45 @@ const S3BucketFileTableDropdown = ({ color, item, globalS3, bucketName }) => {
};
}, []);

const execDownload = async()=>{
const command = new GetObjectCommand({
Bucket: bucketName,
Key: item.Key,
});
const response = await globalS3.send(command);
const arrayBuffer = await response.Body.transformToByteArray();
downloadFile(arrayBuffer, item.Name);
const execDownload = async (setPercentage) => {
const onDownLoadProgress = progress => {
const percentage = Math.round((progress.loaded / item.Size) * 100);
setPercentage(percentage);
};
const result = await downloadFile(item.CID, item.Name, item.Size, onDownLoadProgress, setErr);
return result;
}



const download = async () => {
Emitter.emit('openS3DownloadModal', { name: item.Name, execDownload: execDownload });
Emitter.emit('openS3DownloadModal', { name: item.Name, execDownload: execDownload });
};


const listFilesInBucket = async () => {
const command = new ListObjectsCommand({ Bucket: bucketName, Prefix: item.Key });
const res = await globalS3.send(command);
const { Contents = [] } = res;
const keys = Contents.map((c) => c.Key);
const keys = Contents.map((c) => c.Key);
return keys;

};

const handleRemove = async() => {
};

const handleRemove = async () => {
let keys = [];
if(item.Type === 2){
if (item.Type === 2) {
keys = [item.Key];
await remove(keys);
}else{
} else {
keys = await listFilesInBucket();
await remove(keys);
}
}

}

const remove = async (keys) => {
try {

const deleteObjectsCommand = new DeleteObjectsCommand({
Bucket: bucketName,
Delete: { Objects: keys.map((key) => ({ Key: key })) },
Expand Down Expand Up @@ -137,7 +140,7 @@ const S3BucketFileTableDropdown = ({ color, item, globalS3, bucketName }) => {

const rename = async () => {
console.log("globalS3", item);
Emitter.emit('openS3RenameFileModal', {callBackFn: renameObject });
Emitter.emit('openS3RenameFileModal', { callBackFn: renameObject });
};

const trigger = e => {
Expand Down
36 changes: 22 additions & 14 deletions src/components/Footers/FileControl.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*eslint-disable*/
import React from 'react';
import React, { useState } from 'react';
import { removeFiles } from "services/filesService.js";
import themeStyle from "utils/themeStyle.js";
import Emitter from "utils/eventBus";
import { t } from "utils/text.js";
import * as AWS from "@aws-sdk/client-s3";
import { downloadFile } from 'services/s3Service';
import { downloadFile } from 'services/filesService.js';

const { DeleteObjectsCommand, GetObjectCommand, CopyObjectCommand, ListObjectsCommand } = AWS;
const { DeleteObjectsCommand, CopyObjectCommand, ListObjectsCommand } = AWS;
const s3FileType = 's3File';

let isDelete = false;

export default function FileControl({ itemSelected, unSelect, color, data, type, bucketName, globalS3, prefix }) {

// eslint-disable-next-line no-unused-vars
const [err, setErr] = useState(false);
const hideDownload = type === s3FileType && data.filter(item => item.Type === 1).length > 0;

const listFilesInBucket = async (item) => {
Expand All @@ -25,7 +25,7 @@ export default function FileControl({ itemSelected, unSelect, color, data, type,
};

const handleS3Remove = async () => {
if(isDelete) return;
if (isDelete) return;
isDelete = true;
let keys = [];
if (data.length) {
Expand Down Expand Up @@ -65,16 +65,24 @@ export default function FileControl({ itemSelected, unSelect, color, data, type,
}
};

const execDownload = async () => {

const execDownload = async (setPercentage) => {
const item = data[0];
const command = new GetObjectCommand({
Bucket: bucketName,
Key: item.Key,
});
const response = await globalS3.send(command);
const arrayBuffer = await response.Body.transformToByteArray();
await downloadFile(arrayBuffer, item.Name);
// const command = new GetObjectCommand({
// Bucket: bucketName,
// Key: item.Key,
// });
// const response = await globalS3.send(command);
// const arrayBuffer = await response.Body.transformToByteArray();
// await downloadFile(arrayBuffer, item.Name);

const onDownLoadProgress = progress => {
const percentage = Math.round((progress.loaded / item.Size) * 100);
setPercentage(percentage);
};
const result = await downloadFile(item.CID, item.Name, item.Size, onDownLoadProgress, setErr);
unSelect();
return result;
}

const handleS3Download = () => {
Expand Down
3 changes: 2 additions & 1 deletion src/components/Modals/S3DownloadModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ export default function S3DownloadModal({ color }) {
useEffect(() => {
const set = async function (params) {
console.log('openS3DownloadModal event has occured');
reset();
openModal();
name.current = params.name;
execDownload = params.execDownload;
let result = await execDownload();
let result = await execDownload(setPercentage);
setPercentage(100);
console.log(result);
};
Expand Down

0 comments on commit ac03a17

Please sign in to comment.