33 * Date: 4/02/19 11:04
44 * Copyright 2019 (c) Lightstreams, Palma
55 */
6-
76const ADD_FILE_PATH = `/storage/add` ;
87const ADD_RAW_PATH = `/storage/add-raw` ;
98const UPDATE_FILE_PATH = `/storage/update` ;
109const UPDATE_RAW_PATH = `/storage/update-raw` ;
1110const ADD_FILE_WITH_ACL_PATH = `/storage/add-with-acl` ;
1211const ADD_RAW_WITH_ACL_PATH = `/storage/add-raw-with-acl` ;
1312const FETCH_FILE_PATH = `/storage/fetch` ;
13+ const STREAM_FILE_PATH = `/storage/stream` ;
1414const META_PATH = `/storage/meta` ;
1515
1616const request = require ( '../http/request' ) ;
17+ const { validateCid} = require ( '../leth/cid' ) ;
1718
1819module . exports = ( gwDomain ) => ( {
1920
@@ -28,7 +29,7 @@ module.exports = (gwDomain) => ({
2829 if ( typeof File !== 'undefined' && file instanceof File ) {
2930 const reader = new FileReader ( ) ;
3031 const fileBlob = file . slice ( 0 , file . size ) ;
31- reader . readAsBinaryString ( fileBlob )
32+ reader . readAsBinaryString ( fileBlob ) ;
3233 }
3334 return request . postFile ( `${ gwDomain } ${ ADD_FILE_PATH } ` , {
3435 owner,
@@ -40,22 +41,20 @@ module.exports = (gwDomain) => ({
4041 * Uploaded new file into distributed storage using raw data and fixed file type
4142 * @param owner {string} Address of the owner of the file
4243 * @param password {string} The password that unlocks the owner
43- * @param data {Blob } File content in blob object
44+ * @param rawData {string } File content in blob object
4445 * @param ext {string} Content extension format. For example: '.json', '.png'..
4546 * @returns { meta, acl }
4647 */
47- addRaw : ( owner , password , data , ext ) => {
48- if ( ! data instanceof Blob ) {
49- throw new Exception ( `Argument "data" must be a Blob ` ) ;
48+ addRaw : ( owner , password , rawData , ext ) => {
49+ if ( typeof rawData !== 'string' ) {
50+ throw new Exception ( `Argument "data" must be an string ` ) ;
5051 }
5152
52- return data . text ( ) . then ( rawData => {
53- return request . post ( `${ gwDomain } ${ ADD_RAW_PATH } ` , {
54- owner,
55- password,
56- data : rawData ,
57- ext : ext
58- } ) ;
53+ return request . post ( `${ gwDomain } ${ ADD_RAW_PATH } ` , {
54+ owner,
55+ password,
56+ data : rawData ,
57+ ext : ext ,
5958 } ) ;
6059 } ,
6160
@@ -70,7 +69,7 @@ module.exports = (gwDomain) => ({
7069 if ( typeof File !== 'undefined' && file instanceof File ) {
7170 const reader = new FileReader ( ) ;
7271 const fileBlob = file . slice ( 0 , file . size ) ;
73- reader . readAsBinaryString ( fileBlob )
72+ reader . readAsBinaryString ( fileBlob ) ;
7473 }
7574 return request . postFile ( `${ gwDomain } ${ ADD_FILE_WITH_ACL_PATH } ` , {
7675 owner,
@@ -82,22 +81,20 @@ module.exports = (gwDomain) => ({
8281 * Uploaded new file into distributed storage using raw data and fixed file extension and using an already deployed acl
8382 * @param owner {string} Address of the owner of the file
8483 * @param acl {string} {string} Address to acl contract
85- * @param data {Blob } File content in blob object
84+ * @param rawData {string } File content in blob object
8685 * @param ext {string} Content extension format. For example: '.json', '.png'..
8786 * @returns { meta, acl }
8887 */
89- addRawWithAcl : ( owner , acl , data , ext ) => {
90- if ( ! data instanceof Blob ) {
91- throw new Exception ( `Argument "data" must be a Blob ` ) ;
88+ addRawWithAcl : ( owner , acl , rawData , ext ) => {
89+ if ( typeof rawData !== 'string' ) {
90+ throw new Error ( `Argument "data" must be an string ` ) ;
9291 }
9392
94- return data . text ( ) . then ( rawData => {
95- return request . post ( `${ gwDomain } ${ ADD_RAW_WITH_ACL_PATH } ` , {
96- owner,
97- acl,
98- data : rawData ,
99- ext : ext
100- } ) ;
93+ return request . post ( `${ gwDomain } ${ ADD_RAW_WITH_ACL_PATH } ` , {
94+ owner,
95+ acl,
96+ data : rawData ,
97+ ext : ext ,
10198 } ) ;
10299 } ,
103100
@@ -109,11 +106,14 @@ module.exports = (gwDomain) => ({
109106 * @returns {StreamResponse<{ meta, acl }> } | {<{ meta, acl }>}
110107 */
111108 update : ( owner , meta , file ) => {
109+ validateCid ( meta ) ;
110+
112111 if ( typeof File !== 'undefined' && file instanceof File ) {
113112 const reader = new FileReader ( ) ;
114113 const fileBlob = file . slice ( 0 , file . size ) ;
115- reader . readAsBinaryString ( fileBlob )
114+ reader . readAsBinaryString ( fileBlob ) ;
116115 }
116+
117117 return request . postFile ( `${ gwDomain } ${ UPDATE_FILE_PATH } ` , {
118118 meta,
119119 owner,
@@ -124,22 +124,20 @@ module.exports = (gwDomain) => ({
124124 * Uploaded new file into distributed storage using raw data and fixed file extension
125125 * @param owner {string} Address of the owner of the file
126126 * @param password {string} The password that unlocks the owner
127- * @param data {Blob } File content in blob object
127+ * @param rawData {string } File content in blob object
128128 * @param ext {string} Content extension format. For example: '.json', '.png'..
129129 * @returns { meta, acl }
130130 */
131- updateRaw : ( owner , password , data , ext ) => {
132- if ( ! data instanceof Blob ) {
133- throw new Exception ( `Argument "data" must be a Blob` ) ;
131+ updateRaw : ( owner , password , rawData , ext ) => {
132+ if ( typeof rawData !== 'string' ) {
133+ throw new Error ( `Argument "data" must be a Blob` ) ;
134134 }
135135
136- return data . text ( ) . then ( rawData => {
137- return request . post ( `${ gwDomain } ${ UPDATE_RAW_PATH } ` , {
138- owner,
139- password,
140- data : rawData ,
141- ext : ext
142- } ) ;
136+ return request . post ( `${ gwDomain } ${ UPDATE_RAW_PATH } ` , {
137+ owner,
138+ password,
139+ data : rawData ,
140+ ext : ext ,
143141 } ) ;
144142 } ,
145143
@@ -151,14 +149,17 @@ module.exports = (gwDomain) => ({
151149 * @returns {StreamResponse<**CONTENT_FILE**> } || <**CONTENT_FILE**>
152150 */
153151 fetch : ( meta , token , stream ) => {
154- return request . fetchFile ( `${ gwDomain } ${ FETCH_FILE_PATH } ` , {
155- meta,
156- token
157- } , {
152+ validateCid ( meta ) ;
153+
154+ let reqData = { meta} ;
155+ if ( token ) {
156+ reqData = { ...reqData , token} ;
157+ }
158+ return request . fetchFile ( `${ gwDomain } ${ FETCH_FILE_PATH } ` , reqData , {
158159 stream,
159160 headers : {
160- 'Content-Type' : 'application/json'
161- }
161+ 'Content-Type' : 'application/json' ,
162+ } ,
162163 } ) ;
163164 } ,
164165
@@ -168,24 +169,65 @@ module.exports = (gwDomain) => ({
168169 * @param token {string} Account authentication token
169170 */
170171 fetchUrl : ( meta , token ) => {
171- if ( ! token ) {
172- return `${ gwDomain } ${ FETCH_FILE_PATH } ?meta=${ meta } `
172+ validateCid ( meta ) ;
173+
174+ if ( ! token ) {
175+ return `${ gwDomain } ${ FETCH_FILE_PATH } ?meta=${ meta } ` ;
173176 }
174177 return `${ gwDomain } ${ FETCH_FILE_PATH } ?meta=${ meta } &token=${ encodeURI ( token ) } ` ;
175178 } ,
176179
180+ /**
181+ * Stream file from distributed storage
182+ * @param meta {string} Unique identifier of stored file
183+ * @param token {string} Account authentication token
184+ * @param stream {boolean} Response to be streamed or not
185+ * @returns {StreamResponse<**CONTENT_FILE**> } || <**CONTENT_FILE**>
186+ */
187+ stream : ( meta , token , stream ) => {
188+ validateCid ( meta ) ;
189+
190+ let reqData = { meta} ;
191+ if ( token ) {
192+ reqData = { ...reqData , token} ;
193+ }
194+
195+ return request . fetchFile ( `${ gwDomain } ${ STREAM_FILE_PATH } ` , reqData , {
196+ stream,
197+ headers : {
198+ 'Content-Type' : 'application/json' ,
199+ } ,
200+ } ) ;
201+ } ,
202+
203+ /**
204+ * Return an string with the GET url to fetch content from distributed storage
205+ * @param meta {string} Unique identifier of stored file
206+ * @param token {string} Account authentication token
207+ */
208+ streamUrl : ( meta , token ) => {
209+ validateCid ( meta ) ;
210+
211+ if ( ! token ) {
212+ return `${ gwDomain } ${ STREAM_FILE_PATH } ?meta=${ meta } ` ;
213+ }
214+ return `${ gwDomain } ${ STREAM_FILE_PATH } ?meta=${ meta } &token=${ encodeURI ( token ) } ` ;
215+ } ,
216+
177217 /**
178218 * Fetch metadata information about distributed file
179219 * @param meta {string} Unique identifier of stored file
180220 * @returns {Promise<{ filename, owner, ext, hash, acl, acl, prev_meta_hash }> }
181221 */
182222 meta : ( meta ) => {
223+ validateCid ( meta ) ;
224+
183225 return request . get ( `${ gwDomain } ${ META_PATH } ` , {
184- meta
226+ meta,
185227 } , {
186228 headers : {
187- 'Content-Type' : 'application/json'
188- }
229+ 'Content-Type' : 'application/json' ,
230+ } ,
189231 } ) ;
190232 } ,
191233} ) ;
0 commit comments