Skip to content

Commit

Permalink
Merge pull request #217 from nspcc-dev/feature/214-switch_to_new_rest…
Browse files Browse the repository at this point in the history
…_api
  • Loading branch information
roman-khimov authored Aug 27, 2024
2 parents 9a1098f + 42e8ffb commit 5aa6657
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 200 deletions.
92 changes: 28 additions & 64 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}

function formatBytes(bytes) {
const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
let i = 0
for (i; bytes >= 1024; i += 1) {
bytes /= 1024;
}
return `${bytes === 0 ? bytes : bytes.toFixed(1)} ${units[i]}`;
}

export const App = () => {
const location = useLocation();
const navigate = useNavigate();
Expand All @@ -56,14 +47,6 @@ export const App = () => {
scriptHash: Neon.create.account(process.env.REACT_APP_NEOFS_ACCOUNT).scriptHash,
});

const [ContentTypeHeader] = useState("Content-Type");
const [AuthorizationHeader] = useState("Authorization");
const [BearerOwnerIdHeader] = useState("X-Bearer-Owner-Id");
const [BearerForAllUsers] = useState("X-Bearer-For-All-Users");
const [BearerSignatureHeader] = useState("X-Bearer-Signature");
const [BearerSignatureKeyHeader] = useState("X-Bearer-Signature-Key");
const [BearerLifetime] = useState("X-Bearer-Lifetime");

const [params] = useState({
rest_gw: process.env.REACT_APP_RESTGW ? process.env.REACT_APP_RESTGW : 'https://rest.t5.fs.neo.org/v1',
});
Expand Down Expand Up @@ -353,10 +336,9 @@ export const App = () => {
}

api('POST', '/auth', body, {
[ContentTypeHeader]: "application/json",
[BearerOwnerIdHeader]: walletData.account.address,
[BearerLifetime]: params.objectId ? 24 : 2,
[BearerForAllUsers]: true,
"X-Bearer-Owner-Id": walletData.account.address,
"X-Bearer-Lifetime": params.objectId ? 24 : 2,
"X-Bearer-For-All-Users": true,
}).then((e) => {
onSignMessage(e[0].token, type, operation, params);
});
Expand Down Expand Up @@ -396,10 +378,9 @@ export const App = () => {

if (type === 'object') {
api('GET', '/auth/bearer?walletConnect=true', {}, {
[ContentTypeHeader]: "application/json",
[AuthorizationHeader]: `Bearer ${msg}`,
[BearerSignatureHeader]: response.data + response.salt,
[BearerSignatureKeyHeader]: response.publicKey,
"Authorization": `Bearer ${msg}`,
"X-Bearer-Signature": response.data + response.salt,
"X-Bearer-Signature-Key": response.publicKey,
}).then((e) => {
if (params.objectId) {
onModal('shareObjectLink', { ...params, token: e.token })
Expand All @@ -425,11 +406,10 @@ export const App = () => {
"basicAcl": containerForm.basicAcl,
"attributes": attributes,
}, {
[ContentTypeHeader]: "application/json",
[AuthorizationHeader]: `Bearer ${walletData.tokens.container.PUT.token}`,
[BearerOwnerIdHeader]: walletData.account.address,
[BearerSignatureHeader]: walletData.tokens.container.PUT.signature,
[BearerSignatureKeyHeader]: walletData.publicKey,
"Authorization": `Bearer ${walletData.tokens.container.PUT.token}`,
"X-Bearer-Owner-Id": walletData.account.address,
"X-Bearer-Signature": walletData.tokens.container.PUT.signature,
"X-Bearer-Signature-Key": walletData.publicKey,
}).then((e) => {
if (e.message && e.message.indexOf('insufficient balance to create container') !== -1) {
setLoadingForm(false);
Expand All @@ -451,11 +431,10 @@ export const App = () => {
api('PUT', `/containers/${e.containerId}/eacl?walletConnect=true`, {
"records": containerForm.eACLParams.filter((item) => delete item.isOpen),
}, {
[ContentTypeHeader]: "application/json",
[AuthorizationHeader]: `Bearer ${walletData.tokens.container.SETEACL.token}`,
[BearerOwnerIdHeader]: walletData.account.address,
[BearerSignatureHeader]: walletData.tokens.container.SETEACL.signature,
[BearerSignatureKeyHeader]: walletData.publicKey,
"Authorization": `Bearer ${walletData.tokens.container.SETEACL.token}`,
"X-Bearer-Owner-Id": walletData.account.address,
"X-Bearer-Signature": walletData.tokens.container.SETEACL.signature,
"X-Bearer-Signature-Key": walletData.publicKey,
}).then(() => {
setLoadingForm(false);
onPopup('success', 'New container with EACL has been created');
Expand Down Expand Up @@ -502,11 +481,10 @@ export const App = () => {
setLoadingForm(true);
setError({ active: false, type: [], text: '' });
api('DELETE', `/containers/${containerName}?walletConnect=true`, {}, {
[ContentTypeHeader]: "application/json",
[AuthorizationHeader]: `Bearer ${walletData.tokens.container.DELETE.token}`,
[BearerOwnerIdHeader]: walletData.account.address,
[BearerSignatureHeader]: walletData.tokens.container.DELETE.signature,
[BearerSignatureKeyHeader]: walletData.publicKey,
"Authorization": `Bearer ${walletData.tokens.container.DELETE.token}`,
"X-Bearer-Owner-Id": walletData.account.address,
"X-Bearer-Signature": walletData.tokens.container.DELETE.signature,
"X-Bearer-Signature-Key": walletData.publicKey,
}).then((e) => {
setLoadingForm(false);
if (e.message) {
Expand Down Expand Up @@ -543,16 +521,15 @@ export const App = () => {
setError({ active: false, type: [], text: '' });
setLoadingForm(true);

let formdata = new FormData();
formdata.append('data', objectForm.file);
formdata.append('name', objectForm.name);

const attributesHeaders = {};
attributes.map((attribute) => attributesHeaders[`X-Attribute-${attribute.key}`] = attribute.value);
api('POST', `/upload/${containerId}`, formdata, {
'Content-Type': "multipart/form-data",
[AuthorizationHeader]: `Bearer ${walletData.tokens.object.bearer}`,
...attributesHeaders,
attributes.map((attribute) => attributesHeaders[attribute.key] = attribute.value);
api('POST', `/objects/${containerId}`, objectForm.file, {
'Content-Type': objectForm.file.type,
"Authorization": `Bearer ${walletData.tokens.object.bearer}`,
'X-Attributes': JSON.stringify({
'FileName': objectForm.name,
...attributesHeaders,
}),
}).then((e) => {
setLoadingForm(false);
if (e.message && e.message.indexOf('access to object operation denied') !== -1) {
Expand Down Expand Up @@ -583,12 +560,8 @@ export const App = () => {
const onDeleteObject = (containerId, objectId) => {
setError({ active: false, type: [], text: '' });
setLoadingForm(true);
api('DELETE', `/objects/${containerId}/${objectId}?walletConnect=true`, {}, {
[ContentTypeHeader]: "application/json",
[AuthorizationHeader]: `Bearer ${walletData.tokens.object.token}`,
[BearerOwnerIdHeader]: walletData.account.address,
[BearerSignatureHeader]: walletData.tokens.object.signature,
[BearerSignatureKeyHeader]: walletData.publicKey,
api('DELETE', `/objects/${containerId}/${objectId}`, {}, {
"Authorization": `Bearer ${walletData.tokens.object.bearer}`,
}).then((e) => {
setLoadingForm(false);
if (e.message) {
Expand Down Expand Up @@ -1851,17 +1824,13 @@ export const App = () => {
path="/getobject"
element={<Getobject
onModal={onModal}
formatBytes={formatBytes}
ContentTypeHeader={ContentTypeHeader}
AuthorizationHeader={AuthorizationHeader}
/>}
/>
<Route
path="/profile"
element={<Profile
params={params}
NeoFSContract={NeoFSContract}
formatBytes={formatBytes}
activeNet={activeNet}
onAuth={onAuth}
walletData={walletData}
Expand All @@ -1873,11 +1842,6 @@ export const App = () => {
isLoadContainers={isLoadContainers}
setLoadContainers={setLoadContainers}
onDisconnectWallet={onDisconnectWallet}
ContentTypeHeader={ContentTypeHeader}
AuthorizationHeader={AuthorizationHeader}
BearerOwnerIdHeader={BearerOwnerIdHeader}
BearerSignatureHeader={BearerSignatureHeader}
BearerSignatureKeyHeader={BearerSignatureKeyHeader}
onModal={onModal}
onPopup={onPopup}
/>}
Expand Down
32 changes: 4 additions & 28 deletions src/Components/ContainerItem/ContainerItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import {
import api from '../../api';

export default function ContainerItem({
params,
formatBytes,
containerItem,
onAuth,
walletData,
Expand All @@ -26,11 +24,6 @@ export default function ContainerItem({
index,
isLoadContainers,
setLoadContainers,
ContentTypeHeader,
AuthorizationHeader,
BearerOwnerIdHeader,
BearerSignatureHeader,
BearerSignatureKeyHeader,
}) {
const ObjectsPerPage = 40;
const [isOpen, setIsOpen] = useState(false);
Expand Down Expand Up @@ -90,14 +83,10 @@ export default function ContainerItem({
const onGetObjects = (containerId, pageTemp = pagination.page) => {
setPagination({ ...pagination, page: pageTemp});
setLoadingObjects(true);
api('POST', `/objects/${containerId}/search?walletConnect=true&limit=${ObjectsPerPage}&offset=${pageTemp * ObjectsPerPage}`, {
api('POST', `/objects/${containerId}/search?limit=${ObjectsPerPage}&offset=${pageTemp * ObjectsPerPage}`, {
"filters": [],
}, {
[ContentTypeHeader]: "application/json",
[AuthorizationHeader]: `Bearer ${walletData.tokens.object.token}`,
[BearerOwnerIdHeader]: walletData.account.address,
[BearerSignatureHeader]: walletData.tokens.object.signature,
[BearerSignatureKeyHeader]: walletData.publicKey,
"Authorization": `Bearer ${walletData.tokens.object.bearer}`,
}).then((e) => {
setLoadingObjects(false);
if (e.message) {
Expand All @@ -111,9 +100,8 @@ export default function ContainerItem({

const onGetEACL = (containerId) => {
setLoadingEACL(true);
api('GET', `/containers/${containerId}/eacl?walletConnect=true`, {}, {
[ContentTypeHeader]: "application/json",
[BearerOwnerIdHeader]: walletData.account.address,
api('GET', `/containers/${containerId}/eacl`, {}, {
"X-Bearer-Owner-Id": walletData.account.address,
}).then((e) => {
setLoadingEACL(false);
if (e.records) {
Expand Down Expand Up @@ -240,11 +228,6 @@ export default function ContainerItem({
setLoadContainers={setLoadContainers}
eACLParams={eACLParams}
setEACLParams={setEACLParams}
ContentTypeHeader={ContentTypeHeader}
AuthorizationHeader={AuthorizationHeader}
BearerOwnerIdHeader={BearerOwnerIdHeader}
BearerSignatureHeader={BearerSignatureHeader}
BearerSignatureKeyHeader={BearerSignatureKeyHeader}
/>
)}
</>
Expand Down Expand Up @@ -330,18 +313,11 @@ export default function ContainerItem({
<Heading size={6} weight="light" align="center">Objects not found</Heading>
)}
<TreeView
params={params}
formatBytes={formatBytes}
walletData={walletData}
onModal={onModal}
containerIndex={index}
containerItem={containerItem}
objects={objects}
ContentTypeHeader={ContentTypeHeader}
AuthorizationHeader={AuthorizationHeader}
BearerOwnerIdHeader={BearerOwnerIdHeader}
BearerSignatureHeader={BearerSignatureHeader}
BearerSignatureKeyHeader={BearerSignatureKeyHeader}
/>
{!(pagination.page === 0 && pagination.objects === 0) && (
<div className="pagination">
Expand Down
14 changes: 4 additions & 10 deletions src/Components/EACLPanel/EACLPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ export default function EACLPanel({
setLoadContainers,
eACLParams,
setEACLParams,
ContentTypeHeader,
AuthorizationHeader,
BearerOwnerIdHeader,
BearerSignatureHeader,
BearerSignatureKeyHeader,
isEdit = true,
isErrorParent,
}) {
Expand All @@ -44,11 +39,10 @@ export default function EACLPanel({
api('PUT', `/containers/${containerId}/eacl?walletConnect=true`, {
"records": eACLParams.filter((item) => delete item.isOpen),
}, {
[ContentTypeHeader]: "application/json",
[AuthorizationHeader]: `Bearer ${walletData.tokens.container.SETEACL.token}`,
[BearerOwnerIdHeader]: walletData.account.address,
[BearerSignatureHeader]: walletData.tokens.container.SETEACL.signature,
[BearerSignatureKeyHeader]: walletData.publicKey,
"Authorization": `Bearer ${walletData.tokens.container.SETEACL.token}`,
"X-Bearer-Owner-Id": walletData.account.address,
"X-Bearer-Signature": walletData.tokens.container.SETEACL.signature,
"X-Bearer-Signature-Key": walletData.publicKey,
}).then((e) => {
setLoadingForm(false);
if (e.message) {
Expand Down
Loading

0 comments on commit 5aa6657

Please sign in to comment.