api/jobqueue: add route to upload images

This commit is contained in:
Ondřej Budai 2020-02-10 14:57:56 +01:00 committed by Tom Gundersen
parent 08c5eaf6a6
commit 98aac91083
2 changed files with 76 additions and 0 deletions

View file

@ -100,6 +100,14 @@ func (e *InvalidRequestError) Error() string {
return e.message
}
type NoLocalTargetError struct {
message string
}
func (e *NoLocalTargetError) Error() string {
return e.message
}
func New(stateDir *string, distroArg distro.Distro, distroRegistryArg distro.Registry) *Store {
var s Store
@ -734,6 +742,42 @@ func (s *Store) UpdateImageBuildInCompose(composeID uuid.UUID, imageBuildID int,
})
}
func (s *Store) AddImageToImageUpload(composeID uuid.UUID, imageBuildID int, reader io.Reader) error {
currentCompose, exists := s.Composes[composeID]
if !exists {
return &NotFoundError{"compose does not exist"}
}
imageBuild := currentCompose.ImageBuilds[imageBuildID]
localTarget := imageBuild.GetLocalTarget()
if localTarget == nil {
return &NoLocalTargetError{fmt.Sprintf("image upload requested for compse %s and image build %d but it has no local target", composeID.String(), imageBuildID)}
}
imageType, _ := imageBuild.ImageType.ToCompatString()
filename, _, err := s.distro.FilenameFromType(imageType)
if err != nil {
return &InvalidRequestError{err.Error()}
}
path := fmt.Sprintf("%s/%s", localTarget.Location, filename)
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
_, err = io.Copy(f, reader)
if err != nil {
return err
}
return nil
}
func (s *Store) PushSource(source SourceConfig) {
// FIXME: handle or comment this possible error
_ = s.change(func() error {