-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
172 lines (158 loc) · 4.79 KB
/
index.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var Hyperdrive = require('dat-node')
var crypto = require('crypto')
var fs = require('fs')
var mkdirp = require('mkdirp')
function CabalClientFiles (arg) {
if (!(this instanceof CabalClientFiles)) return new CabalClientFiles(arg)
this.initialStoragePath = arg.storagePath
this.allowSeedingList = arg.allowSeedingList || []
this.hyperdrives = new Map()
}
CabalClientFiles.prototype.storagePath = function (userKey) {
return this.initialStoragePath + userKey + '/'
}
CabalClientFiles.prototype.getDatKeyFromStoragePath = function (userKey) {
var self = this
return new Promise(function (resolve, reject) {
var storagePath = self.storagePath(userKey)
if (fs.existsSync(storagePath)) {
var opts = {
sparse: false,
upload: true,
download: true
}
Hyperdrive(storagePath, opts, function (err, hyperdrive) {
if (err) {
resolve(err)
}
hyperdrive.close()
resolve(hyperdrive.key.toString('hex'))
})
} else {
resolve()
}
})
}
CabalClientFiles.prototype.publish = async function (arg) {
var self = this
var storagePath = self.storagePath(arg.userKey)
var fileDir = crypto.randomBytes(32).toString('hex')
var filePath = storagePath + fileDir + '/'
mkdirp.sync(filePath)
fs.copyFileSync(arg.path, filePath + arg.name)
var datData = {
datKey: arg.datKey,
userKey: arg.userKey
}
if (!self.hyperdrives.has(arg.datKey)) {
console.warn('===> start new seed')
datData = await self.seed(datData)
}
return new Promise(function (resolve, reject) {
var hyperdrive = self.hyperdrives.get(datData.datKey)
hyperdrive.importFiles(function () {
var data = {
datFileName: fileDir + '/' + arg.name,
datKey: datData.datKey
}
resolve(data)
})
})
}
CabalClientFiles.prototype.seed = function (datData) {
var self = this
return new Promise(function (resolve, reject) {
var storagePath = self.storagePath(datData.userKey)
mkdirp.sync(storagePath)
var datArgs = {
sparse: false,
upload: true,
download: true
}
console.warn('===> Seed: start', { datData })
if (datData.datKey) {
datArgs.key = datData.datKey
var hyperdrive = self.hyperdrives.get(datData.datKey)
if (hyperdrive) {
if (hyperdrive.network.connected) {
// Return if already connected swarm of this dat
console.warn('===> Seed: already connected', { datKey: datData.datKey })
datData.hyperdrive = hyperdrive
resolve(datData)
}
} else {
self.hyperdrives.set(datData.datKey, undefined)
}
}
Hyperdrive(storagePath, datArgs, function (err, hyperdrive) {
if (err) throw reject(err)
var datKey = hyperdrive.key.toString('hex')
console.warn('===> Seed: joining Network.....', datKey)
self.hyperdrives.set(datKey, hyperdrive)
hyperdrive.joinNetwork(function () {
datData.datKey = datKey
datData.hyperdrive = hyperdrive
console.warn('===> Seed: joined Network', { datKey, hyperdrive })
resolve(datData)
})
// hyperdrive.network.on('connection', function (connection, info) {
// console.warn('===> Dat: on connection', { connection, info })
// })
// var stats = dat.trackStats()
// var peers = stats.peers
})
})
}
CabalClientFiles.prototype.seedAll = function (allowSeedingList) {
var self = this
allowSeedingList = allowSeedingList || self.allowSeedingList
allowSeedingList.forEach(function (datData) {
self.seed(datData)
})
}
CabalClientFiles.prototype.stopSeeding = function (datKey) {
var self = this
if (datKey) {
// Stop seeding one
var dat = self.hyperdrives.get(datKey)
if (dat) {
dat.close(function () {
self.hyperdrives.delete(datKey)
})
}
} else {
// Stop seeding all
self.hyperdrives.forEach(function (dat, datKey) {
dat.close(function () {
self.hyperdrives.delete(datKey)
})
})
}
}
CabalClientFiles.prototype.fetch = async function (arg) {
var self = this
var datData = {
datKey: arg.datKey,
userKey: arg.userKey
}
var hyperdrive = self.hyperdrives.get(arg.datKey)
if (!hyperdrive) {
console.warn('===> Fetch: connecting...', { arg })
datData = await self.seed(datData)
hyperdrive = datData.hyperdrive
}
return new Promise(function (resolve, reject) {
var storagePath = self.storagePath(arg.userKey)
datData.archive = hyperdrive.archive
datData.localPath = storagePath + arg.fileName
hyperdrive.archive.readFile(arg.fileName, function (err, content) {
if (err) {
reject(err)
}
datData.content = content
console.warn('===> Fetch: readfile...', content)
resolve(datData)
})
})
}
module.exports = CabalClientFiles