@@ -15,6 +15,10 @@ import { ErrorUtils } from '../utils/errors.utils';
1515import { MissingCredentialsError , NotValidDirectoryError , NotValidFolderUuidError } from '../types/command.types' ;
1616import { ValidationService } from '../services/validation.service' ;
1717import { EncryptionVersion } from '@internxt/sdk/dist/drive/storage/types' ;
18+ import { ThumbnailService } from '../services/thumbnail.service' ;
19+ import { BufferStream } from '../utils/stream.utils' ;
20+ import { isFileThumbnailable } from '../utils/thumbnail.utils' ;
21+ import { Readable } from 'node:stream' ;
1822
1923export default class UploadFile extends Command {
2024 static readonly args = { } ;
@@ -52,6 +56,9 @@ export default class UploadFile extends Command {
5256 throw new Error ( 'The file is empty. Uploading empty files is not allowed.' ) ;
5357 }
5458
59+ const fileInfo = path . parse ( filePath ) ;
60+ const fileType = fileInfo . ext . replaceAll ( '.' , '' ) ;
61+
5562 let destinationFolderUuid = await this . getDestinationFolderUuid ( flags [ 'destination' ] , nonInteractive ) ;
5663 if ( destinationFolderUuid . trim ( ) . length === 0 ) {
5764 // destinationFolderUuid is empty from flags&prompt, which means we should use RootFolderUuid
@@ -75,14 +82,22 @@ export default class UploadFile extends Command {
7582 CLIUtils . done ( ) ;
7683
7784 // 2. Upload file to the Network
78- const fileStream = createReadStream ( filePath ) ;
85+ const readStream = createReadStream ( filePath ) ;
7986 const timer = CLIUtils . timer ( ) ;
8087 const progressBar = CLIUtils . progress ( {
8188 format : 'Uploading file [{bar}] {percentage}%' ,
8289 linewrap : true ,
8390 } ) ;
8491 progressBar . start ( 100 , 0 ) ;
8592
93+ let bufferStream : BufferStream | undefined ;
94+ let fileStream : Readable = readStream ;
95+ const isThumbnailable = isFileThumbnailable ( fileType ) ;
96+ if ( isThumbnailable ) {
97+ bufferStream = new BufferStream ( ) ;
98+ fileStream = readStream . pipe ( bufferStream ) ;
99+ }
100+
86101 const progressCallback = ( progress : number ) => {
87102 progressBar . update ( progress * 0.99 ) ;
88103 } ;
@@ -104,10 +119,9 @@ export default class UploadFile extends Command {
104119 const uploadResult = await uploadPromise ;
105120
106121 // 3. Create the file in Drive
107- const fileInfo = path . parse ( filePath ) ;
108122 const createdDriveFile = await DriveFileService . instance . createFile ( {
109123 plain_name : fileInfo . name ,
110- type : fileInfo . ext . replaceAll ( '.' , '' ) ,
124+ type : fileType ,
111125 size : stats . size ,
112126 folder_id : destinationFolderUuid ,
113127 id : uploadResult . fileId ,
@@ -116,6 +130,24 @@ export default class UploadFile extends Command {
116130 name : '' ,
117131 } ) ;
118132
133+ try {
134+ if ( isThumbnailable && bufferStream ) {
135+ const thumbnailBuffer = bufferStream . getBuffer ( ) ;
136+
137+ if ( thumbnailBuffer ) {
138+ await ThumbnailService . instance . uploadThumbnail (
139+ thumbnailBuffer ,
140+ user . bucket ,
141+ user . mnemonic ,
142+ createdDriveFile . id ,
143+ networkFacade ,
144+ ) ;
145+ }
146+ }
147+ } catch ( error ) {
148+ ErrorUtils . report ( this . error . bind ( this ) , error , { command : this . id } ) ;
149+ }
150+
119151 progressBar . update ( 100 ) ;
120152 progressBar . stop ( ) ;
121153
0 commit comments