2024-04-09 22:37:26 +07:00
|
|
|
package api
|
|
|
|
|
2024-04-09 23:17:23 +07:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2024-04-10 17:13:07 +07:00
|
|
|
"net/http"
|
2024-04-09 23:17:23 +07:00
|
|
|
|
|
|
|
"github.com/tigorlazuardi/redmage/db/queries"
|
|
|
|
"github.com/tigorlazuardi/redmage/pkg/errs"
|
|
|
|
)
|
2024-04-09 22:37:26 +07:00
|
|
|
|
|
|
|
type DownloadSubredditParams struct {
|
|
|
|
Countback int
|
|
|
|
NSFW bool
|
2024-04-09 23:17:23 +07:00
|
|
|
Devices []queries.Device
|
2024-04-10 17:13:07 +07:00
|
|
|
Type int
|
2024-04-09 22:37:26 +07:00
|
|
|
}
|
|
|
|
|
2024-04-09 23:17:23 +07:00
|
|
|
var (
|
2024-04-10 17:13:07 +07:00
|
|
|
ErrNoDevices = errors.New("api: no devices set")
|
|
|
|
ErrDownloadDirNotSet = errors.New("api: download directory not set")
|
2024-04-09 23:17:23 +07:00
|
|
|
)
|
|
|
|
|
2024-04-09 22:37:26 +07:00
|
|
|
func (api *API) DownloadSubredditImages(ctx context.Context, subredditName string, params DownloadSubredditParams) error {
|
2024-04-10 17:13:07 +07:00
|
|
|
downloadDir := api.config.String("download.directory")
|
|
|
|
if downloadDir == "" {
|
|
|
|
return errs.Wrapw(ErrDownloadDirNotSet, "download directory must be set before images can be downloaded").Code(http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
2024-04-09 23:17:23 +07:00
|
|
|
if len(params.Devices) == 0 {
|
2024-04-10 17:13:07 +07:00
|
|
|
return errs.Wrapw(ErrNoDevices, "downloading images requires at least one device configured").Code(http.StatusBadRequest)
|
2024-04-09 23:17:23 +07:00
|
|
|
}
|
2024-04-10 17:13:07 +07:00
|
|
|
|
2024-04-09 22:37:26 +07:00
|
|
|
return nil
|
|
|
|
}
|