Skip to content

Commit

Permalink
feat(dcellar-web-ui): remove getsp url by bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
aiden-cao committed Aug 29, 2023
1 parent c8b9ef9 commit c0c6681
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const ActionMenu = memo<ActionMenuProps>(function ActionMenu({
marginRight={'8px'}
onClick={() => onChange(m)}
>
<TransferInIcon color='readable.primary'/>
<TransferInIcon color="#00BA34" />
</ActionButton>
);
case 'transfer_out':
Expand All @@ -87,7 +87,7 @@ export const ActionMenu = memo<ActionMenuProps>(function ActionMenu({
marginRight={'8px'}
onClick={() => onChange(m)}
>
<TransferOutIcon color='readable.primary'/>
<TransferOutIcon color="#00BA34" />
</ActionButton>
);
case 'send':
Expand All @@ -98,7 +98,7 @@ export const ActionMenu = memo<ActionMenuProps>(function ActionMenu({
marginRight={'8px'}
onClick={() => onChange(m)}
>
<SendIcon color='readable.primary'/>
<SendIcon color="#00BA34" />
</ActionButton>
);
// payment account list
Expand Down
189 changes: 111 additions & 78 deletions apps/dcellar-web-ui/src/components/layout/Header/GlobalTasks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import { useChecksumApi } from '@/modules/checksum';
import { useAsyncEffect } from 'ahooks';
import { getDomain } from '@/utils/getDomain';
import { getSpOffChainData } from '@/store/slices/persist';
import { TMakePutObjectHeaders, makePutObjectHeaders } from '@/modules/file/utils/generatePubObjectOptions';
import {
TMakePutObjectHeaders,
makePutObjectHeaders,
} from '@/modules/file/utils/generatePubObjectOptions';
import axios from 'axios';
import { headObject } from '@/facade/object';
import { reverseVisibilityType } from '@/utils/constant';
Expand All @@ -40,11 +43,16 @@ export const GlobalTasks = memo<GlobalTasksProps>(function GlobalTasks() {
const checksumApi = useChecksumApi();
const [counter, setCounter] = useState(0);
const queue = useAppSelector(selectUploadQueue(loginAccount));
const folderInfos = keyBy(queue.filter((q) => q.waitFile.name.endsWith('/')), 'name');
const folderInfos = keyBy(
queue.filter((q) => q.waitFile.name.endsWith('/')),
'name',
);
const upload = queue.filter((t) => t.status === 'UPLOAD');
const ready = queue.filter((t) => {
const isFolder = t.waitFile.name.endsWith('/');
const parentFolder = isFolder ? t.waitFile.name.split('/').slice(0, -1).join('/') + '/' : t.waitFile.relativePath + '/';
const parentFolder = isFolder
? t.waitFile.name.split('/').slice(0, -1).join('/') + '/'
: t.waitFile.relativePath + '/';
if (!folderInfos[parentFolder]) {
return t.status === 'READY';
}
Expand All @@ -68,11 +76,13 @@ export const GlobalTasks = memo<GlobalTasksProps>(function GlobalTasks() {
const res = await checksumApi?.generateCheckSumV2(hashTask.waitFile.file);
console.log('hashing time', performance.now() - a);
if (isEmpty(res)) {
return dispatch(setupUploadTaskErrorMsg({
account: loginAccount,
task: hashTask,
errorMsg: 'calculating hash error',
}))
return dispatch(
setupUploadTaskErrorMsg({
account: loginAccount,
task: hashTask,
errorMsg: 'calculating hash error',
}),
);
}
const { expectCheckSums } = res!;
dispatch(
Expand All @@ -87,10 +97,11 @@ export const GlobalTasks = memo<GlobalTasksProps>(function GlobalTasks() {
// todo refactor
const runUploadTask = async (task: UploadFile) => {
// 1. get approval from sp
const domain = getDomain();
const isFolder = task.waitFile.name.endsWith('/');
const { seedString } = await dispatch(getSpOffChainData(loginAccount, task.spAddress));
const finalName = [...task.prefixFolders, task.waitFile.relativePath, task.waitFile.name].filter(item => !!item).join('/');
const finalName = [...task.prefixFolders, task.waitFile.relativePath, task.waitFile.name]
.filter((item) => !!item)
.join('/');
const createObjectPayload: TBaseGetCreateObject = {
bucketName: task.bucketName,
objectName: finalName,
Expand All @@ -103,16 +114,15 @@ export const GlobalTasks = memo<GlobalTasksProps>(function GlobalTasks() {
const [createObjectTx, _createError] = await genCreateObjectTx(createObjectPayload, {
type: 'ECDSA',
privateKey: tmpAccount.privateKey,
}).then(
resolve,
createTxFault,
);
}).then(resolve, createTxFault);
if (_createError) {
return dispatch(setupUploadTaskErrorMsg({
account: loginAccount,
task,
errorMsg: _createError,
}))
return dispatch(
setupUploadTaskErrorMsg({
account: loginAccount,
task,
errorMsg: _createError,
}),
);
}

const [simulateInfo, simulateError] = await createObjectTx!
Expand All @@ -121,11 +131,13 @@ export const GlobalTasks = memo<GlobalTasksProps>(function GlobalTasks() {
})
.then(resolve, simulateFault);
if (!simulateInfo || simulateError) {
return dispatch(setupUploadTaskErrorMsg({
account: loginAccount,
task,
errorMsg: simulateError,
}))
return dispatch(
setupUploadTaskErrorMsg({
account: loginAccount,
task,
errorMsg: simulateError,
}),
);
}

const broadcastPayload = {
Expand All @@ -140,73 +152,90 @@ export const GlobalTasks = memo<GlobalTasksProps>(function GlobalTasks() {
.broadcast(broadcastPayload)
.then(resolve, broadcastFault);
if (!res || error) {
return dispatch(setupUploadTaskErrorMsg({
account: loginAccount,
task,
errorMsg: error,
}))
return dispatch(
setupUploadTaskErrorMsg({
account: loginAccount,
task,
errorMsg: error,
}),
);
}
const fullObjectName = [...task.prefixFolders, task.waitFile.relativePath, task.waitFile.name].filter(item => !!item).join('/');
const fullObjectName = [...task.prefixFolders, task.waitFile.relativePath, task.waitFile.name]
.filter((item) => !!item)
.join('/');
const payload: TMakePutObjectHeaders = {
bucketName: task.bucketName,
objectName: fullObjectName,
body: task.waitFile.file,
endpoint: spInfo[task.spAddress].endpoint,
txnHash: res.transactionHash,
}
};
const authType = {
type: 'EDDSA',
seed: seedString,
domain: window.location.origin,
address: loginAccount,
} as AuthType;
const [uploadOptions, gpooError] = await makePutObjectHeaders(payload, authType).then(resolve, commonFault);
const [uploadOptions, gpooError] = await makePutObjectHeaders(payload, authType).then(
resolve,
commonFault,
);

if (!uploadOptions || gpooError) {
return dispatch(setupUploadTaskErrorMsg({
account: loginAccount,
task,
errorMsg: gpooError,
}))
return dispatch(
setupUploadTaskErrorMsg({
account: loginAccount,
task,
errorMsg: gpooError,
}),
);
}
const { url, headers } = uploadOptions;
if (isFolder) {
dispatch(updateUploadStatus({
account: loginAccount,
ids: [task.id],
status: 'SEAL',
}));
dispatch(
updateUploadStatus({
account: loginAccount,
ids: [task.id],
status: 'SEAL',
}),
);
} else {
axios.put(url, task.waitFile.file, {
async onUploadProgress(progressEvent) {
const progress = Math.round((progressEvent.loaded / (progressEvent.total as number)) * 100);
await dispatch(progressFetchList(task));
dispatch(updateUploadProgress({ account: loginAccount, id: task.id, progress }));
},

headers: {
Authorization: headers.get('Authorization'),
'content-type': headers.get('content-type'),
'x-gnfd-app-domain': headers.get('x-gnfd-app-domain'),
'x-gnfd-content-sha256': headers.get('x-gnfd-content-sha256'),
'x-gnfd-date': headers.get('x-gnfd-date'),
'x-gnfd-expiry-timestamp': headers.get('x-gnfd-expiry-timestamp'),
'x-gnfd-txn-hash': headers.get('x-gnfd-txn-hash'),
'x-gnfd-user-address': headers.get('x-gnfd-user-address'),
},
}).catch(async (e: Response | any) => {
console.log('upload error', e);
const { message } = await parseErrorXml(e);
setTimeout(() => {
dispatch(setupUploadTaskErrorMsg({
account: loginAccount,
task,
errorMsg: message || e?.message || 'upload error',
}))
}, 200);
})
};
}
axios
.put(url, task.waitFile.file, {
async onUploadProgress(progressEvent) {
const progress = Math.round(
(progressEvent.loaded / (progressEvent.total as number)) * 100,
);
await dispatch(progressFetchList(task));
dispatch(updateUploadProgress({ account: loginAccount, id: task.id, progress }));
},

headers: {
Authorization: headers.get('Authorization'),
'content-type': headers.get('content-type'),
'x-gnfd-app-domain': headers.get('x-gnfd-app-domain'),
'x-gnfd-content-sha256': headers.get('x-gnfd-content-sha256'),
'x-gnfd-date': headers.get('x-gnfd-date'),
'x-gnfd-expiry-timestamp': headers.get('x-gnfd-expiry-timestamp'),
'x-gnfd-txn-hash': headers.get('x-gnfd-txn-hash'),
'x-gnfd-user-address': headers.get('x-gnfd-user-address'),
},
})
.catch(async (e: Response | any) => {
console.log('upload error', e);
const { message } = await parseErrorXml(e);
setTimeout(() => {
dispatch(
setupUploadTaskErrorMsg({
account: loginAccount,
task,
errorMsg: message || e?.message || 'upload error',
}),
);
}, 200);
});
}
};
useAsyncEffect(async () => {
if (!select1Task.length) return;
dispatch(updateUploadStatus({ ids: select1Task, status: 'UPLOAD', account: loginAccount }));
Expand All @@ -221,15 +250,19 @@ export const GlobalTasks = memo<GlobalTasksProps>(function GlobalTasks() {
const _tasks = await Promise.all(
tasks.map(async (task) => {
const { bucketName, prefixFolders, waitFile } = task;
const objectName = [...prefixFolders, waitFile.relativePath, waitFile.name].filter(item => !!item).join('/');
const objectName = [...prefixFolders, waitFile.relativePath, waitFile.name]
.filter((item) => !!item)
.join('/');
const objectInfo = await headObject(bucketName, objectName);

if (!objectInfo || ![0, 1].includes(objectInfo.objectStatus)) {
dispatch(setupUploadTaskErrorMsg({
account: loginAccount,
task,
errorMsg: 'Something went wrong.',
}))
dispatch(
setupUploadTaskErrorMsg({
account: loginAccount,
task,
errorMsg: 'Something went wrong.',
}),
);
return -1;
}
if (objectInfo.objectStatus === 1) {
Expand Down
3 changes: 2 additions & 1 deletion apps/dcellar-web-ui/src/facade/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,15 @@ export const getAuthorizedLink = async (
seedString: string,
view?: '0' | '1',
): Promise<[null, ErrorMsg] | [string]> => {
const { address, objectInfo } = params;
const { address, objectInfo, primarySp } = params;
const { bucketName, objectName } = objectInfo;
const [url, error] = await generateGetObjectOptions({
bucketName,
objectName,
address,
view,
seedString,
endpoint: primarySp.endpoint,
}).then(resolve, commonFault);
if (!url) return [null, error!];
return [url];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box, Flex, Link } from '@totejs/uikit';
import { ColumnProps } from 'antd/es/table';
import React, { useState } from 'react';
import { AlignType, DCTable } from '@/components/common/DCTable';
import { DCTable } from '@/components/common/DCTable';
import { useAppDispatch, useAppSelector } from '@/store';
import { TAccount, setEditOwnerDetail } from '@/store/slices/accounts';
import { isEmpty } from 'lodash-es';
Expand All @@ -19,7 +19,6 @@ const actions: ActionMenuItem[] = [

export const OwnerAccount = () => {
const dispatch = useAppDispatch();
const [rowIndex, setRowIndex] = useState(-1);
const { ownerAccount } = useAppSelector((root) => root.accounts);
const data = ownerAccount?.address ? [ownerAccount] : [];
const router = useRouter();
Expand Down Expand Up @@ -68,12 +67,10 @@ export const OwnerAccount = () => {
},
{
key: 'Operation',
title: 'Operation',
align: 'center' as AlignType,
title: <></>,
width: 200,
render: (_: string, record: TAccount, index: number) => {
const isCurRow = rowIndex === index;
const operations = isCurRow ? ['transfer_in', 'transfer_out', 'send'] : [];
render: (_: string, record: TAccount) => {
const operations = ['transfer_in', 'transfer_out', 'send'];
return (
<ActionMenu
operations={operations}
Expand Down Expand Up @@ -104,12 +101,6 @@ export const OwnerAccount = () => {
onClick: () => {
dispatch(setEditOwnerDetail(record.address));
},
onMouseEnter: () => {
setRowIndex(Number(index));
},
onMouseLeave: () => {
setRowIndex(-1);
},
})}
></DCTable>
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const actions: ActionMenuItem[] = [

export const PaymentAccounts = () => {
const dispatch = useAppDispatch();
const [rowIndex, setRowIndex] = useState(-1);
const router = useRouter();
const { PAList, isLoadingPAList, currentPAPage, ownerAccount } = useAppSelector(
(root) => root.accounts,
Expand Down Expand Up @@ -102,12 +101,10 @@ export const PaymentAccounts = () => {
},
{
key: 'Operation',
title: 'Operation',
align: 'center' as AlignType,
title: <></>,
width: 200,
render: (_: string, record: TAccount, index: number) => {
const isCurRow = rowIndex === index;
const operations = isCurRow ? ['deposit', 'withdraw'] : [];
render: (_: string, record: TAccount) => {
const operations = ['deposit', 'withdraw'];
return (
<ActionMenu
operations={operations}
Expand Down Expand Up @@ -160,12 +157,6 @@ export const PaymentAccounts = () => {
onClick: () => {
dispatch(setEditPaymentDetail(record.address));
},
onMouseEnter: () => {
setRowIndex(Number(index));
},
onMouseLeave: () => {
setRowIndex(-1);
},
})}
></DCTable>
</>
Expand Down
Loading

0 comments on commit c0c6681

Please sign in to comment.