1- import Uppy , { BasePlugin , UppyFile } from '@uppy/core'
1+ import Uppy , { BasePlugin } from '@uppy/core'
22import { basename , dirname , join } from 'path'
33import { v4 as uuidV4 } from 'uuid'
44import { Language } from 'vue3-gettext'
@@ -13,11 +13,15 @@ import {
1313 SpacesStore ,
1414 UserStore ,
1515 locationPublicLink ,
16- formatFileSize
16+ formatFileSize ,
17+ OcUppyFile ,
18+ OcUppyMeta ,
19+ OcUppyBody
1720} from '@ownclouders/web-pkg'
18- import { locationSpacesGeneric , UppyService , UppyResource } from '@ownclouders/web-pkg'
21+ import { locationSpacesGeneric , UppyService } from '@ownclouders/web-pkg'
1922import { isPersonalSpaceResource , isShareSpaceResource } from '@ownclouders/web-client'
2023import { ClientService , queryItemAsString } from '@ownclouders/web-pkg'
24+ import { PluginOpts } from '@uppy/core/lib/BasePlugin'
2125
2226export interface HandleUploadOptions {
2327 clientService : ClientService
@@ -44,7 +48,7 @@ export interface HandleUploadOptions {
4448 * 4. create directory tree if needed
4549 * 5. start upload
4650 */
47- export class HandleUpload extends BasePlugin {
51+ export class HandleUpload extends BasePlugin < PluginOpts , OcUppyMeta , OcUppyBody > {
4852 clientService : ClientService
4953 language : Language
5054 route : Ref < RouteLocationNormalizedLoaded >
@@ -58,7 +62,7 @@ export class HandleUpload extends BasePlugin {
5862 directoryTreeCreateEnabled : boolean
5963 conflictHandlingEnabled : boolean
6064
61- constructor ( uppy : Uppy , opts : HandleUploadOptions ) {
65+ constructor ( uppy : Uppy < OcUppyMeta , OcUppyBody > , opts : HandleUploadOptions ) {
6266 super ( uppy , opts )
6367 this . id = opts . id || 'HandleUpload'
6468 this . type = 'modifier'
@@ -81,7 +85,7 @@ export class HandleUpload extends BasePlugin {
8185 this . handleUpload = this . handleUpload . bind ( this )
8286 }
8387
84- removeFilesFromUpload ( filesToUpload : UppyResource [ ] ) {
88+ removeFilesFromUpload ( filesToUpload : OcUppyFile [ ] ) {
8589 for ( const file of filesToUpload ) {
8690 this . uppy . removeFile ( file . id )
8791 }
@@ -104,8 +108,8 @@ export class HandleUpload extends BasePlugin {
104108 /**
105109 * Converts the input files type UppyResources and updates the uppy upload queue
106110 */
107- prepareFiles ( files : UppyFile [ ] , uploadFolder : Resource ) : UppyResource [ ] {
108- const filesToUpload : Record < string , UppyResource > = { }
111+ prepareFiles ( files : OcUppyFile [ ] , uploadFolder : Resource ) : OcUppyFile [ ] {
112+ const filesToUpload : Record < string , OcUppyFile > = { }
109113
110114 if ( ! this . resourcesStore . currentFolder && unref ( this . route ) ?. params ?. token ) {
111115 // public file drop
@@ -127,7 +131,7 @@ export class HandleUpload extends BasePlugin {
127131 uploadId : uuidV4 ( )
128132 }
129133
130- filesToUpload [ file . id ] = file as unknown as UppyResource
134+ filesToUpload [ file . id ] = file
131135 }
132136 this . uppy . setState ( { files : { ...this . uppy . getState ( ) . files , ...filesToUpload } } )
133137 return Object . values ( filesToUpload )
@@ -138,7 +142,7 @@ export class HandleUpload extends BasePlugin {
138142 const topLevelFolderIds : Record < string , string > = { }
139143
140144 for ( const file of files ) {
141- const relativeFilePath = ( file . meta . relativePath || file . meta . webkitRelativePath ) as string
145+ const relativeFilePath = file . meta . relativePath
142146 // Directory without filename
143147 const directory =
144148 ! relativeFilePath || dirname ( relativeFilePath ) === '.' ? '' : dirname ( relativeFilePath )
@@ -166,7 +170,7 @@ export class HandleUpload extends BasePlugin {
166170 ...file . meta ,
167171 // file data
168172 name : file . name ,
169- mtime : file . data . lastModified / 1000 ,
173+ mtime : ( file . data as File ) . lastModified / 1000 ,
170174 // current path & space
171175 spaceId : unref ( this . space ) . id ,
172176 spaceName : unref ( this . space ) . name ,
@@ -186,27 +190,25 @@ export class HandleUpload extends BasePlugin {
186190 routeShareId : queryItemAsString ( query ?. shareId ) || ''
187191 }
188192
189- filesToUpload [ file . id ] = file as unknown as UppyResource
193+ filesToUpload [ file . id ] = file
190194 }
191195
192196 this . uppy . setState ( { files : { ...this . uppy . getState ( ) . files , ...filesToUpload } } )
193197 return Object . values ( filesToUpload )
194198 }
195199
196- checkQuotaExceeded ( filesToUpload : UppyResource [ ] ) : boolean {
200+ checkQuotaExceeded ( filesToUpload : OcUppyFile [ ] ) : boolean {
197201 let quotaExceeded = false
198202
199- const uploadSizeSpaceMapping = filesToUpload . reduce ( ( acc , uppyResource ) => {
203+ const uploadSizeSpaceMapping = filesToUpload . reduce ( ( acc , uppyFile ) => {
200204 let targetUploadSpace : SpaceResource
201205
202- if ( uppyResource . meta . routeName === locationPublicLink . name ) {
206+ if ( uppyFile . meta . routeName === locationPublicLink . name ) {
203207 return acc
204208 }
205209
206- if ( uppyResource . meta . routeName === locationSpacesGeneric . name ) {
207- targetUploadSpace = this . spacesStore . spaces . find (
208- ( { id } ) => id === uppyResource . meta . spaceId
209- )
210+ if ( uppyFile . meta . routeName === locationSpacesGeneric . name ) {
211+ targetUploadSpace = this . spacesStore . spaces . find ( ( { id } ) => id === uppyFile . meta . spaceId )
210212 }
211213
212214 if (
@@ -219,7 +221,7 @@ export class HandleUpload extends BasePlugin {
219221 }
220222
221223 const existingFile = this . resourcesStore . resources . find (
222- ( c ) => ! uppyResource . meta . relativeFolder && c . name === uppyResource . name
224+ ( c ) => ! uppyFile . meta . relativeFolder && c . name === uppyFile . name
223225 )
224226 const existingFileSize = existingFile ? Number ( existingFile . size ) : 0
225227
@@ -230,12 +232,12 @@ export class HandleUpload extends BasePlugin {
230232 if ( ! matchingMappingRecord ) {
231233 acc . push ( {
232234 space : targetUploadSpace ,
233- uploadSize : uppyResource . data . size - existingFileSize
235+ uploadSize : uppyFile . data . size - existingFileSize
234236 } )
235237 return acc
236238 }
237239
238- matchingMappingRecord . uploadSize = uppyResource . data . size - existingFileSize
240+ matchingMappingRecord . uploadSize = uppyFile . data . size - existingFileSize
239241
240242 return acc
241243 } , [ ] )
@@ -274,9 +276,9 @@ export class HandleUpload extends BasePlugin {
274276 * Creates the directory tree and removes files of failed directories from the upload queue.
275277 */
276278 async createDirectoryTree (
277- filesToUpload : UppyResource [ ] ,
279+ filesToUpload : OcUppyFile [ ] ,
278280 uploadFolder : Resource
279- ) : Promise < UppyResource [ ] > {
281+ ) : Promise < OcUppyFile [ ] > {
280282 const { webdav } = this . clientService
281283 const space = unref ( this . space )
282284 const { id : currentFolderId , path : currentFolderPath } = uploadFolder
@@ -307,7 +309,7 @@ export class HandleUpload extends BasePlugin {
307309 const uploadId = ! isRoot ? uuidV4 ( ) : topLevelIds [ path ]
308310 const relativeFolder = dirname ( path ) === '/' ? '' : dirname ( path )
309311
310- const uppyResource = {
312+ const uppyFile = {
311313 id : uuidV4 ( ) ,
312314 name : basename ( path ) ,
313315 isFolder : true ,
@@ -332,22 +334,22 @@ export class HandleUpload extends BasePlugin {
332334 return
333335 }
334336
335- this . uppyService . publish ( 'addedForUpload' , [ uppyResource ] )
337+ this . uppyService . publish ( 'addedForUpload' , [ uppyFile ] )
336338
337339 try {
338340 const folder = await webdav . createFolder ( space , {
339341 path : urlJoin ( currentFolderPath , path ) ,
340342 fetchFolder : isRoot
341343 } )
342344 this . uppyService . publish ( 'uploadSuccess' , {
343- ...uppyResource ,
344- meta : { ...uppyResource . meta , fileId : folder ?. fileId }
345+ ...uppyFile ,
346+ meta : { ...uppyFile . meta , fileId : folder ?. fileId }
345347 } )
346348 } catch ( error ) {
347349 if ( error . statusCode !== 405 ) {
348350 console . error ( error )
349351 failedFolders . push ( path )
350- this . uppyService . publish ( 'uploadError' , { file : uppyResource , error } )
352+ this . uppyService . publish ( 'uploadError' , { file : uppyFile , error } )
351353 }
352354 }
353355 }
@@ -380,7 +382,7 @@ export class HandleUpload extends BasePlugin {
380382 * The handler that prepares all files to be uploaded and goes through all necessary steps.
381383 * Eventually triggers to upload in Uppy.
382384 */
383- async handleUpload ( files : UppyFile [ ] ) {
385+ async handleUpload ( files : OcUppyFile [ ] ) {
384386 if ( ! files . length ) {
385387 return
386388 }
@@ -415,7 +417,7 @@ export class HandleUpload extends BasePlugin {
415417 }
416418
417419 filesToUpload = result
418- const conflictMap = result . reduce < Record < string , UppyResource > > ( ( acc , file ) => {
420+ const conflictMap = result . reduce < Record < string , OcUppyFile > > ( ( acc , file ) => {
419421 acc [ file . id ] = file
420422 return acc
421423 } , { } )
0 commit comments