fic: add createBucketIfNotExists helper

This commit is contained in:
Derrick Hammer 2024-01-15 14:25:12 -05:00
parent dc239b0cba
commit d4ed4eb9a2
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 35 additions and 0 deletions

View File

@ -42,6 +42,11 @@ func (s StorageServiceImpl) PutFile(file io.ReadSeeker, bucket string, generateP
buf.Reset() buf.Reset()
err = s.createBucketIfNotExists(bucket)
if err != nil {
return nil, err
}
resp, err := s.httpApi.R(). resp, err := s.httpApi.R().
SetPathParam("path", hashStr). SetPathParam("path", hashStr).
SetFormData(map[string]string{ SetFormData(map[string]string{
@ -69,3 +74,33 @@ func (s *StorageServiceImpl) Init() {
s.httpApi = client s.httpApi = client
} }
func (s *StorageServiceImpl) createBucketIfNotExists(bucket string) error {
resp, err := s.httpApi.R().
SetPathParam("bucket", bucket).
Get("/api/bus/bucket/{bucket}")
if err != nil {
return err
}
if resp.StatusCode() != 404 {
if resp.IsError() && resp.Error() != nil {
return resp.Error().(error)
}
} else {
resp, err := s.httpApi.R().
SetBody(map[string]string{
"bucket": bucket,
}).
Post("/api/bus/bucket")
if err != nil {
return err
}
if resp.IsError() && resp.Error() != nil {
return resp.Error().(error)
}
}
return nil
}