summaryrefslogtreecommitdiff
path: root/store/image.go
diff options
context:
space:
mode:
Diffstat (limited to 'store/image.go')
-rw-r--r--store/image.go74
1 files changed, 25 insertions, 49 deletions
diff --git a/store/image.go b/store/image.go
index d1b7549..6ef019d 100644
--- a/store/image.go
+++ b/store/image.go
@@ -31,15 +31,13 @@ type Image struct {
func (s *Store) Images() []string {
var images []string
if entries, err := os.ReadDir(s.ImagesDir()); err != nil {
- log.Fatalf("Error while reading first level image directory, %s", err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error reading first level image directory, %s", err))
} else {
for _, entry := range entries {
if entry.IsDir() {
var subEntries []os.DirEntry
if subEntries, err = os.ReadDir(s.ImagesDir() + "/" + entry.Name()); err != nil {
- log.Fatalf("Error while reading second level image directory %s, %s", entry.Name(), err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error reading second level image directory %s, %s", entry.Name(), err))
} else {
for _, subEntry := range subEntries {
images = append(images, entry.Name()+subEntry.Name())
@@ -66,12 +64,10 @@ func (s *Store) Image(hash string) Image {
func (s *Store) ImageMetadataRead(path string) Image {
var metadata Image
if payload, err := os.ReadFile(path); err != nil {
- log.Fatalf("Error while reading image metadata %s, %s", path, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error reading image metadata %s, %s", path, err))
} else {
if err = json.Unmarshal(payload, &metadata); err != nil {
- log.Fatalf("Error while parsing image metadata %s, %s", path, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error parsing image metadata %s, %s", path, err))
}
}
return metadata
@@ -87,12 +83,10 @@ func (s *Store) ImageData(hash string, preview bool) (Image, []byte) {
defer s.getLock(hash).RUnlock()
var metadata Image
if payload, err := os.ReadFile(s.ImageMetadataPath(hash)); err != nil {
- log.Fatalf("Error while reading image %s metadata, %s", hash, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error reading image %s metadata, %s", hash, err))
} else {
if err = json.Unmarshal(payload, &metadata); err != nil {
- log.Fatalf("Error while parsing image %s metadata, %s", hash, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error parsing image %s metadata, %s", hash, err))
}
}
var path string
@@ -102,8 +96,7 @@ func (s *Store) ImageData(hash string, preview bool) (Image, []byte) {
path = s.ImagePreviewFilePath(hash)
}
if data, err := os.ReadFile(path); err != nil {
- log.Fatalf("Error while reading image %s file, %s", hash, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error reading image %s file, %s", hash, err))
} else {
return metadata, data
}
@@ -124,8 +117,7 @@ func (s *Store) ImageTags(flake string) []string {
func (s *Store) imageTags(flake string) []string {
var tags []string
if entries, err := os.ReadDir(s.ImageTagsPath(flake)); err != nil {
- log.Fatalf("Error while reading tags of image %s, %s", flake, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error reading tags of image %s, %s", flake, err))
} else {
for _, entry := range entries {
tags = append(tags, entry.Name())
@@ -158,7 +150,7 @@ func (s *Store) ImageAdd(data []byte, flake string) Image {
// Decode image and set format
var img image.Image
if i, format, err := image.Decode(bytes.NewReader(data)); err != nil {
- log.Warnf("Error while decoding upload %s, %s", info.Hash, err)
+ log.Warnf("Error decoding upload %s, %s", info.Hash, err)
return Image{}
} else {
img = resize.Thumbnail(256, 256, i, resize.Bilinear)
@@ -167,39 +159,32 @@ func (s *Store) ImageAdd(data []byte, flake string) Image {
// Create image directory and tags
if err := os.MkdirAll(s.ImageHashTagsPath(info.Hash), s.PermissionDir); err != nil {
- log.Fatalf("Error while creating image %s directory, %s", info.Hash, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error creating image %s directory, %s", info.Hash, err))
}
// Generate and save image metadata
if payload, err := json.Marshal(info); err != nil {
- log.Fatalf("Error while encoding metadata of image %s, %s", info.Hash, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error encoding metadata of image %s, %s", info.Hash, err))
} else {
if err = os.WriteFile(s.ImageMetadataPath(info.Hash), payload, s.PermissionFile); err != nil {
- log.Fatalf("Error while saving metadata of image %s, %s", info.Hash, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error saving metadata of image %s, %s", info.Hash, err))
}
}
// Save image
if err := os.WriteFile(s.ImageFilePath(info.Hash), data, s.PermissionFile); err != nil {
- log.Fatalf("Error while saving image %s, %s", info.Hash, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error saving image %s, %s", info.Hash, err))
}
// Save preview image
if preview, err := os.Create(s.ImagePreviewFilePath(info.Hash)); err != nil {
- log.Fatalf("Error while creating image %s preview, %s", info.Hash, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error creating image %s preview, %s", info.Hash, err))
} else {
if err = jpeg.Encode(preview, img, &jpeg.Options{Quality: 100}); err != nil {
- log.Fatalf("Error while saving image %s preview, %s", info.Hash, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error saving image %s preview, %s", info.Hash, err))
}
if err = preview.Close(); err != nil {
- log.Fatalf("Error while closing image %s preview, %s", info.Hash, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error closing image %s preview, %s", info.Hash, err))
}
}
@@ -233,12 +218,10 @@ func (s *Store) ImageSource(hash, source string) {
// Set source and save
info.Source = source
if payload, err := json.Marshal(info); err != nil {
- log.Fatalf("Error while encoding metadata of image %s while updating source, %s", hash, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error encoding metadata of image %s while updating source, %s", hash, err))
} else {
if err = os.WriteFile(s.ImageMetadataPath(hash), payload, s.PermissionFile); err != nil {
- log.Fatalf("Error while saving metadata of image %s while updating source, %s", hash, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error saving metadata of image %s while updating source, %s", hash, err))
}
}
log.Infof("Image hash %s source set to %s.", hash, source)
@@ -248,8 +231,7 @@ func (s *Store) ImageSource(hash, source string) {
func (s *Store) ImageSnowflakes() []string {
var snowflakes []string
if entries, err := os.ReadDir(s.ImagesSnowflakeDir()); err != nil {
- log.Fatalf("Error while reading snowflakes, %s", err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error reading snowflakes, %s", err))
} else {
for _, entry := range entries {
snowflakes = append(snowflakes, entry.Name())
@@ -264,8 +246,7 @@ func (s *Store) ImageSnowflakeHash(flake string) string {
return s.ImageMetadataRead(s.ImageSnowflakePath(flake) + "/" + infoJson).Hash
} else {
if path, err := os.ReadFile(s.ImageSnowflakePath(flake)); err != nil {
- log.Fatalf("Error while reading snowflake %s association file, %s", flake, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error reading snowflake %s association file, %s", flake, err))
} else {
return s.ImageMetadataRead(string(path) + "/" + infoJson).Hash
}
@@ -297,20 +278,17 @@ func (s *Store) ImageDestroy(hash string) {
// Remove snowflake
if err := os.Remove(s.ImageSnowflakePath(info.Snowflake)); err != nil {
- log.Fatalf("Error while destroying snowflake %s of image %s, %s", info.Snowflake, hash, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error destroying snowflake %s of image %s, %s", info.Snowflake, hash, err))
}
// Disassociate user
if err := os.Remove(s.UserImagesPath(info.User) + "/" + info.Snowflake); err != nil {
- log.Fatalf("Error while destroying association %s with user %s, %s.", info.Snowflake, info.User, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error destroying association %s with user %s, %s.", info.Snowflake, info.User, err))
}
// Remove data directory
if err := os.RemoveAll(s.ImagePath(hash)); err != nil {
- log.Fatalf("Error while destroying image %s, %s", hash, err)
- log.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error destroying image %s, %s", hash, err))
}
// Register remove counter
@@ -349,14 +327,12 @@ func (s *Store) ImageTagRemove(flake, tag string) {
func (s *Store) imageTagRemove(flake, tag string) {
if s.file(s.ImageTagsPath(flake) + "/" + tag) {
if err := os.Remove(s.ImageTagsPath(flake) + "/" + tag); err != nil {
- log.Fatalf("Error while unreferencing image %s from tag %s, %s", flake, tag, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error unreferencing image %s from tag %s, %s", flake, tag, err))
}
}
if s.file(s.TagPath(tag) + "/" + flake) {
if err := os.Remove(s.TagPath(tag) + "/" + flake); err != nil {
- log.Fatalf("Error while unreferencing tag %s from image %s, %s", tag, flake, err)
- os.Exit(1)
+ s.fatalClose(fmt.Sprintf("Error unreferencing tag %s from image %s, %s", tag, flake, err))
}
}
s.PageRegisterRemove("tag_"+tag, flake)