-
-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
googleai: move downloadImageData to separate file (#530)
- Loading branch information
Showing
4 changed files
with
39 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package googleai | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// downloadImageData downloads the content from the given URL and returns the | ||
// image type and data. The image type is the second part of the response's | ||
// MIME (e.g. "png" from "image/png"). | ||
func downloadImageData(url string) (string, []byte, error) { | ||
resp, err := http.Get(url) //nolint | ||
if err != nil { | ||
return "", nil, fmt.Errorf("failed to fetch image from url: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
urlData, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", nil, fmt.Errorf("failed to read image bytes: %w", err) | ||
} | ||
|
||
mimeType := resp.Header.Get("Content-Type") | ||
|
||
parts := strings.Split(mimeType, "/") | ||
if len(parts) != 2 { | ||
return "", nil, ErrInvalidMimeType | ||
} | ||
|
||
return parts[1], urlData, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
//nolint:gomnd | ||
package googleai | ||
|
||
// options is a set of options for GoogleAI clients. | ||
|