@@ -86,67 +86,22 @@ module.exports = function FileServer(protocol, zeronet) {
8686 if ( ! zeronet . zites [ data . site ] ) return cb ( new Error ( "Unknown site" ) )
8787 //FIXME: tmp hack. can be easily abused for force-seeding
8888 const zite = zeronet . zites [ data . site ]
89- const fs = zite . fs
90- //if (data.location % FILE_CHUNK) return cb(new Error("Currently supports only full chunks (" + FILE_CHUNK + ")"))
91- //const chunkid = data.location / FILE_CHUNK
92- fs . getFile ( data . inner_path , ( err , stream ) => {
93- if ( err ) return cb ( err )
94- //let leftover = Buffer.from("")
95- //let size = 0
96- pull (
97- stream ,
98- pull . collect ( ( err , chunks ) => {
99- if ( err ) return cb ( err )
100- const file = Buffer . concat ( chunks )
101- if ( file . length < data . location ) return cb ( new Error ( "Oversize" ) )
102- return cb ( null , {
103- body : file . slice ( data . location , data . location + FILE_CHUNK ) ,
104- location : data . location + file . slice ( data . location , data . location + FILE_CHUNK ) . length ,
105- size : file . length
106- } )
89+ log ( "got a getFile for %s@%s" , data . site , data . inner_path )
90+ const stream = zite . tree . storage . readStream ( data . site , 0 , data . inner_path )
91+ pull (
92+ stream ,
93+ pull . collect ( ( err , chunks ) => {
94+ log ( "got OUT a getFile for %s@%s" , data . site , data . inner_path , err , chunks )
95+ if ( err ) return cb ( err )
96+ const file = Buffer . concat ( chunks )
97+ if ( file . length < data . location ) return cb ( new Error ( "Oversize" ) )
98+ return cb ( null , {
99+ body : file . slice ( data . location , data . location + FILE_CHUNK ) ,
100+ location : data . location + file . slice ( data . location , data . location + FILE_CHUNK ) . length ,
101+ size : file . length
107102 } )
108- /*queue((end, data, cb) => {
109- console.log(data, leftover)
110- const rt = () => {
111- let send = []
112- data = Buffer.concat([leftover, data])
113- if (data.length < FILE_CHUNK) return send
114- if (data.length == FILE_CHUNK) {
115- send.push(data)
116- leftover = Buffer.from("")
117- return send
118- }
119- while (data.length >= FILE_CHUNK) {
120- send.push(data.slice(0, FILE_CHUNK))
121- data = data.slice(FILE_CHUNK + 1)
122- }
123- leftover = data
124- return send
125- }
126- if (end && leftover.length) {
127- cb(null, [leftover])
128- leftover = null
129- } else if (end) {
130- cb(end)
131- } else {
132- size += data.length
133- cb(null, rt())
134- }
135- }, {
136- sendMultiple: true
137- }),
138- pull.collect((err, chunks) => {
139- if (err) return cb(err)
140- if (chunkid * FILE_CHUNK > size) return cb(new Error("Oversize"))
141- console.log(chunks[chunkid], chunks)
142- return cb(null, {
143- body: chunks[chunkid],
144- location: data.location + chunks[chunkid].length,
145- size
146- })
147- })*/
148- )
149- } )
103+ } )
104+ )
150105 } )
151106
152107 protocol . handle ( "hasZite" , { in : {
0 commit comments