55 "encoding/json"
66 "fmt"
77 "net/http"
8- "strings"
98 "time"
109
1110 "github.com/uploadcare/uploadcare-cli/internal/output"
@@ -18,7 +17,6 @@ import (
1817type fileService struct {
1918 sdkFileSvc file.Service
2019 sdkUploadSvc upload.Service
21- cdnBase string
2220 verbose * output.VerboseLogger
2321}
2422
@@ -30,12 +28,14 @@ func NewFileService(publicKey, secretKey, cdnBase string, httpClient *http.Clien
3028 PublicKey : publicKey ,
3129 SecretKey : secretKey ,
3230 }
33- conf := & ucare.Config {
34- APIVersion : ucare .APIv07 ,
35- SignBasedAuthentication : true ,
36- CDNBase : cdnBase ,
37- HTTPClient : httpClient ,
38- UserAgent : UserAgent ,
31+ conf , err := ucare .NewConfig (creds ,
32+ ucare .WithSignBasedAuthentication (),
33+ ucare .WithCDNBase (cdnBase ),
34+ ucare .WithHTTPClient (httpClient ),
35+ ucare .WithUserAgent (UserAgent ),
36+ )
37+ if err != nil {
38+ return nil , err
3939 }
4040 client , err := ucare .NewClient (creds , conf )
4141 if err != nil {
@@ -44,21 +44,10 @@ func NewFileService(publicKey, secretKey, cdnBase string, httpClient *http.Clien
4444 return & fileService {
4545 sdkFileSvc : file .NewService (client ),
4646 sdkUploadSvc : upload .NewService (client ),
47- cdnBase : cdnBase ,
4847 verbose : verbose ,
4948 }, nil
5049}
5150
52- // setCDNURL overwrites OriginalFileURL with a URL built from the configured
53- // CDN base and the file's UUID. This makes --cdn-base and per-project CDN
54- // domains actually take effect, since the API always returns the legacy
55- // ucarecdn.com URL regardless of project settings.
56- func (s * fileService ) setCDNURL (f * service.File ) {
57- if s .cdnBase != "" && f .OriginalFileURL != "" {
58- f .OriginalFileURL = strings .TrimRight (s .cdnBase , "/" ) + "/" + f .UUID + "/"
59- }
60- }
61-
6251func (s * fileService ) Info (ctx context.Context , uuid string , includeAppData bool ) (* service.File , error ) {
6352 var params * file.InfoParams
6453 if includeAppData {
@@ -69,9 +58,7 @@ func (s *fileService) Info(ctx context.Context, uuid string, includeAppData bool
6958 if err != nil {
7059 return nil , err
7160 }
72- f := mapFileInfo (info )
73- s .setCDNURL (f )
74- return f , nil
61+ return mapFileInfo (info ), nil
7562}
7663
7764func mapFileInfo (info file.Info ) * service.File {
@@ -145,9 +132,7 @@ func (s *fileService) List(ctx context.Context, opts service.FileListOptions) (*
145132 if err != nil {
146133 return nil , err
147134 }
148- f := mapFileInfo (* info )
149- s .setCDNURL (f )
150- files = append (files , * f )
135+ files = append (files , * mapFileInfo (* info ))
151136 }
152137
153138 return & service.FileListResult {
@@ -171,9 +156,7 @@ func (s *fileService) Iterate(ctx context.Context, opts service.FileListOptions,
171156 if err != nil {
172157 return err
173158 }
174- f := mapFileInfo (* info )
175- s .setCDNURL (f )
176- if err := fn (* f ); err != nil {
159+ if err := fn (* mapFileInfo (* info )); err != nil {
177160 return err
178161 }
179162 }
@@ -304,36 +287,25 @@ func (s *fileService) enrichUploadInfo(ctx context.Context, id string, fallback
304287 fileInfo , err := s .sdkFileSvc .Info (ctx , id , nil )
305288 if err != nil {
306289 s .verbose .Infof ("file info fetch failed, using upload response: %v" , err )
307- s .setCDNURL (fallback )
308290 return fallback
309291 }
310- f := mapFileInfo (fileInfo )
311- s .setCDNURL (f )
312- return f
292+ return mapFileInfo (fileInfo )
313293}
314294
315295func (s * fileService ) Store (ctx context.Context , uuids []string ) (* service.BatchResult , error ) {
316296 batch , err := s .sdkFileSvc .BatchStore (ctx , uuids )
317297 if err != nil {
318298 return nil , err
319299 }
320- result := mapBatchInfo (batch )
321- for i := range result .Files {
322- s .setCDNURL (& result .Files [i ])
323- }
324- return result , nil
300+ return mapBatchInfo (batch ), nil
325301}
326302
327303func (s * fileService ) Delete (ctx context.Context , uuids []string ) (* service.BatchResult , error ) {
328304 batch , err := s .sdkFileSvc .BatchDelete (ctx , uuids )
329305 if err != nil {
330306 return nil , err
331307 }
332- result := mapBatchInfo (batch )
333- for i := range result .Files {
334- s .setCDNURL (& result .Files [i ])
335- }
336- return result , nil
308+ return mapBatchInfo (batch ), nil
337309}
338310
339311func mapBatchInfo (batch file.BatchInfo ) * service.BatchResult {
@@ -361,9 +333,7 @@ func (s *fileService) LocalCopy(ctx context.Context, params service.LocalCopyPar
361333 if err != nil {
362334 return nil , err
363335 }
364- f := mapFileInfo (copyInfo .Result )
365- s .setCDNURL (f )
366- return f , nil
336+ return mapFileInfo (copyInfo .Result ), nil
367337}
368338
369339func (s * fileService ) RemoteCopy (ctx context.Context , params service.RemoteCopyParams ) (* service.RemoteCopyResult , error ) {
0 commit comments