-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipfs.js
135 lines (131 loc) · 5.1 KB
/
ipfs.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import ipfsAPI from 'ipfs-api'
import ipfs from '../util/ipfs'
import Vue from 'vue';
import pipe from 'it-pipe'
if (process.server) {
const fs = require('fs');
// some code with fs
}
export default {
state: () => ({
textFile: null,
content: null,
index: null
}),
mutations: {
getFile: (state, data) => {
state.textFile = data.content.bookContent
state.content = data.content
state.index = data.index
// console.log(state.textFile)
console.log(state.content)
console.log(state.index)
console.log('getFile')
},
clearBookContent: (state) => {
state.textFile = null
state.content = null
state.index = null
console.log(state.textFile)
console.log(state.content)
console.log('clearBookContent')
},
},
actions: {
publish: async({state, commit, dispatch}, content) => {
console.log(content)
let bookHash, imgHash, metadataHash
//uploading book content to ipfs
await ipfs.files.add(content.file, (err, files) => {
if (err) { throw err }
console.log(files)
bookHash = files[0].hash
//uploading book cover image to ipfs
ipfs.files.add(content.image, (err, files) => {
if (err) { throw err }
console.log(files)
imgHash = files[0].hash
console.log(bookHash)
console.log(imgHash)
const metadata = JSON.stringify({
title: content.title,
imageHash: imgHash
})
console.log(metadata)
let metadataBuffer = Buffer.from(metadata)
console.log(metadataBuffer)
let temp = JSON.parse(metadataBuffer.toString())
console.log(temp)
//uploading metadata to ipfs
ipfs.files.add(metadataBuffer, (err, files) => {
if (err) { throw err }
console.log(files)
metadataHash = files[0].hash
console.log(metadataHash)
dispatch('web3/publish', {content, bookHash, metadataHash}, { root: true })
})
})
})
},
requestContent: async({state, commit, dispatch}, hash) => {
let textFile
let ipfsPath = `https://ipfs.io/ipfs/${hash}`
console.log(ipfsPath);
ipfs.cat(hash, function(err, res) {
if(err || !res) return console.error("ipfs cat error", err, res);
if(res.readable) {
console.error('unhandled: cat result is a pipe', res);
} else {
console.log(res)
textFile = res.toString()
commit('getFile', textFile)
}
})
},
getBookContent: async({state, commit, dispatch}, bookHash) => {
return ipfs.cat(bookHash)
// let textFile
// let ipfsPath = `https://ipfs.io/ipfs/${hash}`
// console.log(ipfsPath);
// ipfs.cat(hash, function(err, res) {
// if(err || !res) return console.error("ipfs cat error", err, res);
// if(res.readable) {
// console.error('unhandled: cat result is a pipe', res);
// } else {
// console.log(res)
// textFile = res.toString()
// commit('getFile', textFile)
// }
// })
},
getMetadata: async({state, commit, dispatch}, metadataHash) => {
return ipfs.cat(metadataHash)
},
uploadAnnotations: async({state, rootState, commit, dispatch}, data) => {
let index = data.index
let annotations = data.content.annotations
let tokenId = data.content.tokenId
annotations.push({
id: annotations.length+1,
from: rootState.web3.currentAccount,
annotation: data.annotation
})
console.log(annotations)
let annotationBuffer = Buffer.from(JSON.stringify(annotations))
console.log(annotationBuffer)
let temp = JSON.parse(annotationBuffer.toString())
console.log(temp)
//uploading annotations to ipfs
ipfs.files.add(annotationBuffer, (err, res) => {
if (err) { throw err }
console.log(res)
let annotationHash = res[0].hash
console.log(annotationHash)
dispatch('web3/setAnnotationHash', {index, annotationHash, tokenId}, { root: true })
})
},
getAnnotations: async({state, commit, dispatch}, annotationHash) => {
return ipfs.cat(annotationHash)
},
}
}