-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepdata.js
85 lines (77 loc) · 3.19 KB
/
prepdata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import shell from 'shelljs'
import fs from 'fs'
import {SpheronClient, ProtocolEnum} from '@spheron/storage'
import dotenv from 'dotenv'
import path from 'path'
import CID from 'cids'
import {ethers} from 'ethers'
dotenv.config()
const token = process.env.SPHERON_TOKEN
const client = new SpheronClient({ token });
export const doUpload = async (filePath) => {
let currentlyUploaded = 0
const { uploadId, bucketId, protocolLink, dynamicLinks } = await client.upload(
filePath,
{
protocol: ProtocolEnum.IPFS,
name: 'fvm_bucket',
onUploadInitiated: (uploadId) => {
console.log(`Upload with id ${uploadId} started...`);
},
onChunkUploaded: (uploadedSize, totalSize) => {
currentlyUploaded += uploadedSize;
console.log(`Uploaded ${currentlyUploaded} of ${totalSize} Bytes.`);
},
}
)
return protocolLink
}
export const generatePrepData = async(filename) => {
const cmd = `generate-car/generate-car --single -i ${filename} -o out -p ${path.dirname(filename)}/`
try {
if (filename){
if (fs.statSync(filename).isFile()) {
fs.mkdirSync('out')
if (fs.existsSync('out')){
const dataObj = {}
const result = shell.exec(cmd).stdout
const data = JSON.parse(result)
dataObj.pieceSize = data.PieceSize
dataObj.size = data.Ipld.Link[0].Size
const cidHexRaw = new CID(data.PieceCid).toString('base16').substring(1) //convert to hex
//const cid = ethers.hexlify(data.PieceCid)
//console.log('Hexify:', cid)
const cidHex = "0x" + cidHexRaw //hex notation
dataObj.pieceCid = cidHex
//dataObj.pieceCid = data.PieceCid
dataObj.dataCid = data.DataCid
const carFile = fs.readdirSync('out')
if (fs.statSync('out/'+carFile[0]).isFile()) {
const link = await doUpload('out/'+carFile[0])
if (link) {
dataObj.carlink = link
} else {
console.log('Error: Something went wrong')
}
fs.unlinkSync('out/'+carFile[0])
fs.rmdirSync('out')
console.log(dataObj)
return dataObj
}
else {
console.log('Car file was not generated')
}
}
else {
console.log('Internal error')
}
}
else {
console.error('Provide a valid file')
}
}
} catch (e) {
console.error(e)
}
}
//generatePrepData('istockphoto.jpg')