worker.go 705 B

1234567891011121314151617181920212223242526
  1. package service
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net/http"
  6. "one-api/common"
  7. "one-api/constant"
  8. "strings"
  9. )
  10. func DoImageRequest(originUrl string) (resp *http.Response, err error) {
  11. if constant.EnableWorker() {
  12. common.SysLog(fmt.Sprintf("downloading image from worker: %s", originUrl))
  13. workerUrl := constant.WorkerUrl
  14. if !strings.HasSuffix(workerUrl, "/") {
  15. workerUrl += "/"
  16. }
  17. // post request to worker
  18. data := []byte(`{"url":"` + originUrl + `","key":"` + constant.WorkerValidKey + `"}`)
  19. return http.Post(constant.WorkerUrl, "application/json", bytes.NewBuffer(data))
  20. } else {
  21. common.SysLog(fmt.Sprintf("downloading image from origin: %s", originUrl))
  22. return http.Get(originUrl)
  23. }
  24. }