files.go 832 B

123456789101112131415161718192021222324252627282930313233343536
  1. package api
  2. import (
  3. "context"
  4. "github.com/ente-io/cli/utils/constants"
  5. "github.com/spf13/viper"
  6. "strconv"
  7. "strings"
  8. )
  9. var (
  10. downloadHost = "https://files.ente.io/?fileID="
  11. )
  12. func downloadUrl(fileID int64) string {
  13. apiEndpoint := viper.GetString("endpoint.api")
  14. if apiEndpoint == "" || strings.Compare(apiEndpoint, constants.EnteApiUrl) == 0 {
  15. return downloadHost + strconv.FormatInt(fileID, 10)
  16. }
  17. return apiEndpoint + "/files/download/" + strconv.FormatInt(fileID, 10)
  18. }
  19. func (c *Client) DownloadFile(ctx context.Context, fileID int64, absolutePath string) error {
  20. req := c.downloadClient.R().
  21. SetContext(ctx).
  22. SetOutput(absolutePath)
  23. attachToken(req)
  24. r, err := req.Get(downloadUrl(fileID))
  25. if r.IsError() {
  26. return &ApiError{
  27. StatusCode: r.StatusCode(),
  28. Message: r.String(),
  29. }
  30. }
  31. return err
  32. }