@@ -11,6 +11,7 @@ import (
1111 "github.com/forceu/gokapi/internal/encryption/end2end"
1212 "github.com/forceu/gokapi/internal/helper"
1313 "github.com/forceu/gokapi/internal/models"
14+ "github.com/schollz/progressbar/v3"
1415 "io"
1516 "mime/multipart"
1617 "net/http"
@@ -44,7 +45,7 @@ func Init(url, key string, end2endKey []byte) {
4445}
4546
4647func GetVersion () (string , int , error ) {
47- result , err := getUrl (gokapiUrl + "/info/version" , []header {})
48+ result , err := getUrl (gokapiUrl + "/info/version" , []header {}, false )
4849 if err != nil {
4950 return "" , 0 , err
5051 }
@@ -61,7 +62,7 @@ func GetVersion() (string, int, error) {
6162}
6263
6364func GetConfig () (int , int , bool , error ) {
64- result , err := getUrl (gokapiUrl + "/info/config" , []header {})
65+ result , err := getUrl (gokapiUrl + "/info/config" , []header {}, false )
6566 if err != nil {
6667 return 0 , 0 , false , err
6768 }
@@ -78,9 +79,13 @@ func GetConfig() (int, int, bool, error) {
7879 return parsedResult .MaxFilesize , parsedResult .MaxChunksize , parsedResult .EndToEndEncryptionEnabled , nil
7980}
8081
81- func getUrl (url string , headers []header ) (string , error ) {
82+ func getUrl (url string , headers []header , longTimeout bool ) (string , error ) {
83+ timeout := 30 * time .Second
84+ if longTimeout {
85+ timeout = 30 * time .Minute
86+ }
8287 client := & http.Client {
83- Timeout : time . Second * 10 ,
88+ Timeout : timeout ,
8489 }
8590 req , err := http .NewRequest ("GET" , url , nil )
8691 if err != nil {
@@ -108,6 +113,7 @@ func getUrl(url string, headers []header) (string, error) {
108113}
109114
110115func UploadFile (uploadParams cliflags.UploadConfig ) (models.FileApiOutput , error ) {
116+ var progressBar * progressbar.ProgressBar
111117 file , err := os .OpenFile (uploadParams .File , os .O_RDONLY , 0664 )
112118 if err != nil {
113119 fmt .Println ("ERROR: Could not open file to upload" )
@@ -137,6 +143,10 @@ func UploadFile(uploadParams cliflags.UploadConfig) (models.FileApiOutput, error
137143 }
138144 uuid := helper .GenerateRandomString (30 )
139145
146+ if ! uploadParams .JsonOutput {
147+ progressBar = progressbar .DefaultBytes (- 1 , "uploading" )
148+ }
149+
140150 if isE2e {
141151 cipher , err := encryption .GetRandomCipher ()
142152 if err != nil {
@@ -147,12 +157,12 @@ func UploadFile(uploadParams cliflags.UploadConfig) (models.FileApiOutput, error
147157 return models.FileApiOutput {}, err
148158 }
149159 for i := int64 (0 ); i < sizeBytes ; i = i + (int64 (chunkSize ) * megaByte ) {
150- err = uploadChunk (stream , uuid , i , int64 (chunkSize )* megaByte , sizeBytes )
160+ err = uploadChunk (stream , uuid , i , int64 (chunkSize )* megaByte , sizeBytes , progressBar )
151161 if err != nil {
152162 return models.FileApiOutput {}, err
153163 }
154164 }
155- metaData , err := completeChunk (uuid , "Encrypted File" , sizeBytes , realSize , true , uploadParams )
165+ metaData , err := completeChunk (uuid , "Encrypted File" , sizeBytes , realSize , true , uploadParams , progressBar )
156166 if err != nil {
157167 return models.FileApiOutput {}, err
158168 }
@@ -174,12 +184,12 @@ func UploadFile(uploadParams cliflags.UploadConfig) (models.FileApiOutput, error
174184 }
175185
176186 for i := int64 (0 ); i < sizeBytes ; i = i + (int64 (chunkSize ) * megaByte ) {
177- err = uploadChunk (file , uuid , i , int64 (chunkSize )* megaByte , sizeBytes )
187+ err = uploadChunk (file , uuid , i , int64 (chunkSize )* megaByte , sizeBytes , progressBar )
178188 if err != nil {
179189 return models.FileApiOutput {}, err
180190 }
181191 }
182- metaData , err := completeChunk (uuid , nameToBase64 (file ), sizeBytes , realSize , false , uploadParams )
192+ metaData , err := completeChunk (uuid , nameToBase64 (file ), sizeBytes , realSize , false , uploadParams , progressBar )
183193 if err != nil {
184194 return models.FileApiOutput {}, err
185195 }
@@ -194,7 +204,7 @@ func getFileName(f *os.File) string {
194204 return filepath .Base (f .Name ())
195205}
196206
197- func uploadChunk (f io.Reader , uuid string , offset , chunkSize , filesize int64 ) error {
207+ func uploadChunk (f io.Reader , uuid string , offset , chunkSize , filesize int64 , progressBar * progressbar. ProgressBar ) error {
198208 body := new (bytes.Buffer )
199209 writer := multipart .NewWriter (body )
200210 part , err := writer .CreateFormFile ("file" , "uploadedfile" )
@@ -227,7 +237,14 @@ func uploadChunk(f io.Reader, uuid string, offset, chunkSize, filesize int64) er
227237 return err
228238 }
229239
230- r , err := http .NewRequest ("POST" , gokapiUrl + "/chunk/add" , body )
240+ var bodyReader io.Reader
241+ if progressBar != nil {
242+ bodyReader = io .TeeReader (body , progressBar )
243+ } else {
244+ bodyReader = body
245+ }
246+
247+ r , err := http .NewRequest ("POST" , gokapiUrl + "/chunk/add" , bodyReader )
231248 if err != nil {
232249 return err
233250 }
@@ -253,10 +270,16 @@ func uploadChunk(f io.Reader, uuid string, offset, chunkSize, filesize int64) er
253270 return nil
254271}
255272
256- func completeChunk (uid , filename string , filesize , realsize int64 , useE2e bool , uploadParams cliflags.UploadConfig ) (models.FileApiOutput , error ) {
273+ func completeChunk (uid , filename string , filesize , realsize int64 , useE2e bool , uploadParams cliflags.UploadConfig , progressBar * progressbar. ProgressBar ) (models.FileApiOutput , error ) {
257274 type expectedFormat struct {
258275 FileInfo models.FileApiOutput `json:"FileInfo"`
259276 }
277+ if progressBar != nil {
278+ _ = progressBar .Finish ()
279+ }
280+ if ! uploadParams .JsonOutput {
281+ fmt .Println ("Finalising..." )
282+ }
260283 result , err := getUrl (gokapiUrl + "/chunk/complete" , []header {
261284 {"uuid" , uid },
262285 {"filename" , filename },
@@ -266,8 +289,8 @@ func completeChunk(uid, filename string, filesize, realsize int64, useE2e bool,
266289 {"allowedDownloads" , strconv .Itoa (uploadParams .ExpiryDownloads )},
267290 {"expiryDays" , strconv .Itoa (uploadParams .ExpiryDays )},
268291 {"password" , uploadParams .Password },
269- {"contenttype" , "application/octet-stream" }, // TODO
270- })
292+ {"contenttype" , "application/octet-stream" },
293+ }, true )
271294 if err != nil {
272295 return models.FileApiOutput {}, err
273296 }
@@ -282,7 +305,7 @@ func completeChunk(uid, filename string, filesize, realsize int64, useE2e bool,
282305func GetE2eInfo () (models.E2EInfoPlainText , error ) {
283306 var result models.E2EInfoEncrypted
284307 var fileInfo models.E2EInfoPlainText
285- resultJson , err := getUrl (gokapiUrl + "/e2e/get" , []header {})
308+ resultJson , err := getUrl (gokapiUrl + "/e2e/get" , []header {}, false )
286309 if err != nil {
287310 return models.E2EInfoPlainText {}, err
288311 }
0 commit comments