Files
cocos/pkg/resource/http.go
T
Sammy Kerata Oina c59a413765
CI / lint (push) Has been cancelled
CI / test (agent) (push) Has been cancelled
CI / test (cli) (push) Has been cancelled
CI / test (cmd) (push) Has been cancelled
CI / test (internal) (push) Has been cancelled
CI / test (manager, true) (push) Has been cancelled
CI / test (pkg) (push) Has been cancelled
CI / upload-coverage (push) Has been cancelled
NOISSUE - Implement extensible resource downloader framework with support for S3, GCS, and OCI sources (#590)
* feat: implement extensible resource downloader framework with support for S3, GCS, and OCI sources

Signed-off-by: SammyOina <sammyoina@gmail.com>

* refactor: improve resource URL parsing and add support for bare OCI image references

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* fix: add empty string check and slash requirement for OCI image inference, and update python unit tests with event mock expectations

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* refactor: introduce OCIClient interface, add test coverage for decryption, and improve resource download error handling

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* chore: remove trailing whitespace in OCI downloader and HTTP tests

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

---------

Signed-off-by: SammyOina <sammyoina@gmail.com>
Signed-off-by: Sammy Oina <sammyoina@gmail.com>
2026-04-28 11:21:03 +02:00

90 lines
2.0 KiB
Go

// Copyright (c) Ultraviolet
// SPDX-License-Identifier: Apache-2.0
package resource
import (
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"
)
const (
// SourceTypeHTTPS represents an HTTPS resource source.
SourceTypeHTTPS = "https"
// SourceTypeHTTP represents an HTTP resource source.
SourceTypeHTTP = "http"
httpTimeout = 5 * time.Minute
)
// HTTPDownloader downloads resources via HTTP/HTTPS.
type HTTPDownloader struct {
client *http.Client
sourceTyp string
}
// NewHTTPSDownloader creates a new HTTPS downloader.
func NewHTTPSDownloader() *HTTPDownloader {
return &HTTPDownloader{
client: &http.Client{
Timeout: httpTimeout,
},
sourceTyp: SourceTypeHTTPS,
}
}
// NewHTTPDownloader creates a new HTTP downloader (insecure, for testing).
func NewHTTPDownloader() *HTTPDownloader {
return &HTTPDownloader{
client: &http.Client{
Timeout: httpTimeout,
},
sourceTyp: SourceTypeHTTP,
}
}
// Download fetches a resource from an HTTP/HTTPS URL and writes it to destPath.
func (h *HTTPDownloader) Download(ctx context.Context, url string, destPath string) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("failed to create HTTP request: %w", err)
}
resp, err := h.client.Do(req)
if err != nil {
return fmt.Errorf("HTTP request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("HTTP request returned status %d: %s", resp.StatusCode, resp.Status)
}
// Ensure parent directory exists.
if err := os.MkdirAll(filepath.Dir(destPath), 0o755); err != nil {
return fmt.Errorf("failed to create destination directory: %w", err)
}
f, err := os.Create(destPath)
if err != nil {
return fmt.Errorf("failed to create destination file: %w", err)
}
defer f.Close()
if _, err := io.Copy(f, resp.Body); err != nil {
return fmt.Errorf("failed to write response body: %w", err)
}
return nil
}
// Type returns the source type identifier.
func (h *HTTPDownloader) Type() string {
return h.sourceTyp
}