@@ -2,9 +2,7 @@ const frictionlessCkanMapper = require('frictionless-ckan-mapper-js')
22const axios = require ( 'axios' )
33
44const CkanAuthApi = require ( './util/ckan-auth-api' )
5- const CkanUploadAPI = require ( './util/ckan-upload-api' )
6- const ActionApi = require ( './util/action-api' )
7- const Open = require ( './file' )
5+ const LfsClient = require ( './lfs-client' )
86
97const { camelToSnakeCase } = require ( './util/general' )
108
@@ -16,15 +14,15 @@ const { camelToSnakeCase } = require('./util/general')
1614 * @param {string } api
1715 */
1816class Client {
19- constructor ( apiKey , organizationId , datasetId , api , lfs ) {
17+ constructor ( apiKey , organizationId , datasetId , api , lfsServerUrl ) {
2018 this . apiKey = apiKey
2119 this . organizationId = organizationId
2220 this . datasetId = datasetId
2321 this . api = api
24- this . lfs = lfs
22+ this . lfsServerUrl = lfsServerUrl
2523 }
2624
27- async doBlobAuthz ( ) {
25+ async getUploadAuthToken ( ) {
2826 // Create the scope to send to CkanAuthz
2927 let scope = [ `obj:${ this . organizationId } /${ this . datasetId } /*:write` ]
3028
@@ -144,63 +142,31 @@ class Client {
144142 }
145143
146144 /**
147- * The result of push blob method
148- * @typedef { Object } PushBlobResult
149- * @property { string } oid - oid
150- * @property { number } size - size of the file
151- * @property { string } name - resource name
152- * @property { boolean } success - Indicates whether the request was successful or not
153- * @property { boolean } fileExists - Indicates whether the resource exists or not
154- */
155- /* *
156- * @param {Object } resource - This datajs resource. Please check https://github.com/datopian/data.js
157- * @param {function } onProgress a callback function to track the progress
158- * @return {Promise<PushBlobResult> } request result
145+ * Upload a blob to storage
146+ *
147+ * This will return a promise that can be resolve to 'true' if the file has
148+ * been uploaded successfully, or `false` if the file was not uploaded
149+ * because it already exists in storage. Either case can be considered a
150+ * success.
151+ *
152+ * Any kind of failure to upload will trigger an exception.
153+ *
154+ * @param {File } resource
155+ * @param {CallableFunction } onProgress
156+ * @returns {Promise<boolean> }
159157 */
160158 async pushBlob ( resource , onProgress ) {
161159 // Get the JWT token
162- const token = await this . doBlobAuthz ( )
163-
164- // Given the JWT token and file size, will return signed URL, verify URL and JWT token
165- const lfs = await CkanAuthApi . requestFileUploadActions (
166- this . lfs ,
167- token ,
168- resource . descriptor . hash ,
169- resource . size ,
170- this . organizationId ,
171- this . datasetId
172- )
173- const object = lfs . objects [ 0 ]
174- const result = {
175- oid : object . oid ,
176- size : object . size ,
177- name : resource . descriptor . name ,
178- success : true ,
179- fileExists : false ,
180- }
160+ const token = await this . getUploadAuthToken ( )
181161
182- // Upload the file to cloud storage
183- if ( object . actions ) {
184- await CkanUploadAPI . pushDataToBlobStorage (
185- object . actions . upload ,
186- resource ,
187- onProgress
188- )
189- await CkanUploadAPI . verifyUpload (
190- lfs . objects [ 0 ] . actions . verify ,
191- resource . descriptor . hash ,
192- resource . size
193- )
194- return result
195- } else {
196- // File is already in storage
197- result . fileExists = true
198- return result
199- }
162+ const lfsClient = new LfsClient . GitLfsClient ( this . lfsServerUrl , {
163+ "Authorization" : `Bearer ${ token } `
164+ } )
165+
166+ return await lfsClient . upload ( resource , this . organizationId , this . datasetId , onProgress ) ;
200167 }
201168}
202169
203170module . exports = {
204171 Client,
205- Open,
206172}
0 commit comments