Skip to content

Commit

Permalink
fix: query file info bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fage2022 committed Aug 28, 2024
1 parent 987e509 commit 795cb77
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 29 deletions.
16 changes: 8 additions & 8 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author": "CESS <[email protected]>",
"homepage": "https://www.cess.cloud",
"license": "MIT",
"version": "0.2.8",
"version": "0.2.9",
"description": "A js-sdk for Cess Project with file storage",
"main": "./dist/index.js",
"module": "./dist/index.js",
Expand Down
72 changes: 56 additions & 16 deletions src/api/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import * as fileHelper from "../util/file-helper.js";
import bs58 from "bs58";
import { hexToString } from "@polkadot/util";
import { formatterSize } from "../util/formatter.js";
import { queryBlockHeight, blockHeightToDatetime } from "../util/index.js";
import moment from "moment";


export default class File extends ControlBase {
constructor(api, keyring, gatewayURL = "http://deoss-pub-gateway.cess.cloud/", isDebug = false) {
Expand All @@ -28,9 +31,9 @@ export default class File extends ControlBase {
file.fileName = owe.fileName;
file.bucketName = owe.bucketName;
}
file.fileSize = tmp.data.fileSize;
file.fileSizeStr = formatterSize(tmp.data.fileSize);
file.stat = tmp.data.stat;
file.fileSize = tmp.data?.fileSize;
file.fileSizeStr = formatterSize(tmp.data?.fileSize);
file.stat = tmp.data?.stat;
}
}
return ret;
Expand All @@ -51,8 +54,8 @@ export default class File extends ControlBase {
let data = ret.toJSON();
data.forEach((t, i) => {
t.fileHash = data2[i].fileHash;
t.fileConsumeSpace = t.fileSize;
t.fileConsumeSpaceStr = formatterSize(t.fileSize);
t.fileConsumeSpace = t?.fileSize;
t.fileConsumeSpaceStr = formatterSize(t?.fileSize);
});
return {
msg: "ok",
Expand All @@ -76,16 +79,35 @@ export default class File extends ControlBase {
if (data && data.owner && data.owner.length > 0) {
for (let i = 0; i < data.owner.length; i++) {
let n = hu.owner[i].fileName;
let territoryName = hu.owner[i].territoryName;
if (n.indexOf("0x") == 0) {
try {
n = hexToString(n);
} catch (e) {
console.error(e);
}
}
data.owner[i].fileName = n;
if (territoryName.indexOf("0x") == 0) {
try {
territoryName = hexToString(territoryName);
} catch (e) {
console.error(e);
}
}
if (n) {
data.owner[i].fileName = n;
data.title = n;
data.fileName = n;
}
data.owner[i].bucketName = hu.owner[i].bucketName;
data.owner[i].territoryName = territoryName;
data.bucketName = hu.owner[i].bucketName;
data.territoryName = territoryName;
}
data.fid = fileHash;
data.fileSizeStr = formatterSize(data.fileSize);
data.uploadTime = await blockHeightToDatetime(this.api, data.completion);
delete data.segmentList;
}
return {
msg: "ok",
Expand All @@ -94,28 +116,36 @@ export default class File extends ControlBase {
} catch (error) {
console.error(error);
return {
msg: "ok",
msg: "error",
errMsg: error.message,
error: JSON.stringify(error),
};
}
}
async queryFileInfo(fileHash) {
async queryFileDealMap(fileHash) {
try {
let ret = await this.api.query.fileBank.dealMap(fileHash);
if (!ret) {
return { msg: 'ok' };
return { msg: 'file not found' };
}
let json = ret.toJSON();
let hu = ret.toHuman();
if (!json) {
return { msg: 'file not found' };
}
let currBlockHeight = await queryBlockHeight(this.api);
let data = {
fid: fileHash,
fileSize: json.fileSize,
fileSizeStr: formatterSize(json.fileSize),
user: hu.user.user,
bucketName: hu.user.bucketName,
fileName: hu.user.fileName,
territoryName: hu.user.territoryName,
fileSize: json?.fileSize,
fileSizeStr: formatterSize(json?.fileSize),
owner: [hu.user],
bucketName: hu.user?.bucketName,
fileName: hu.user?.fileName,
title: hu.user?.fileName,
territoryName: hu.user?.territoryName,
stat: "Pending",
completion: currBlockHeight,
uploadTime: moment().format("YYYY-MM-DD HH:mm:ss")
};
return {
msg: "ok",
Expand All @@ -124,12 +154,22 @@ export default class File extends ControlBase {
} catch (error) {
console.error(error);
return {
msg: "ok",
msg: "error",
errMsg: error.message,
error: JSON.stringify(error),
};
}
}
async queryFileInfo(fileHash) {
let ret = await this.queryFileMetadata(fileHash);
// console.log("queryFileMetadata", ret);
if (ret.msg == 'ok' && ret.data) {
return ret;
}
ret = await this.queryFileDealMap(fileHash);
// console.log("queryFileDealMap", ret);
return ret;
}

async uploadFile(accountId32, fileObj, territory, progressCb = null, message = null, sign = null, acc, evmacc, blockIndex) {
try {
Expand Down
5 changes: 5 additions & 0 deletions src/control-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,9 @@ export default class ControlBase {
// return pair.address;

}
async queryBlockHeight() {
let ret = await this.api.query.system.number();
let blockHeight = ret.toJSON();
return blockHeight;
}
};
3 changes: 3 additions & 0 deletions src/util/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ function formatterSize(size) {
}

function formatterSize2(size) {
if (!size) {
return { size: 0, ext: "B" };
}
let sizeNum = _.isString(size) ? _.toNumber(size) : size;
const sizeUnits = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];

Expand Down
17 changes: 13 additions & 4 deletions src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
*/
import bs58 from "bs58";
import moment from "moment";

const uint8ArrayToHex = (bytes) =>
"0x" + bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
Expand Down Expand Up @@ -91,16 +92,23 @@ function getDataIfOk(result) {
}
function sleep(minisec) {
return new Promise((resolve, reject) => {
setTimeout(function () {
resolve();
}, minisec);
setTimeout(function () {
resolve();
}, minisec);
});
}
async function queryBlockHeight(api) {
let ret = await api.query.system.number();
let blockHeight = ret.toJSON();
return blockHeight;
}
async function blockHeightToDatetime(api, blockHeight) {
if (!blockHeight) {
return moment().format("YYYY-MM-DD HH:mm:ss");
}
let currBlockHeight = await queryBlockHeight(api);
return moment().add(6 * (blockHeight - currBlockHeight), "s").format("YYYY-MM-DD HH:mm:ss");
}

export {
base58ToIP,
Expand All @@ -112,5 +120,6 @@ export {
uint8ArrayToIP,
uint8ArrayToString,
sleep,
queryBlockHeight
queryBlockHeight,
blockHeightToDatetime
};

0 comments on commit 795cb77

Please sign in to comment.