2026-03-12 23:49:16 +00:00
|
|
|
package file
|
2026-02-08 04:36:12 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
)
|
|
|
|
|
|
2026-04-01 21:23:28 +00:00
|
|
|
func ImageFileFromReader(collection Collection, uid uuid.UUID, body io.Reader) error {
|
2026-04-16 19:49:18 +00:00
|
|
|
filepath := fileContentPathUUID(collection, uid)
|
2026-02-08 04:36:12 +00:00
|
|
|
|
|
|
|
|
// Create file in configured directory
|
|
|
|
|
dst, err := os.Create(filepath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Failed to create image file %s: %w", filepath, err)
|
|
|
|
|
}
|
|
|
|
|
defer dst.Close()
|
|
|
|
|
|
|
|
|
|
// Copy rest of request body to file
|
|
|
|
|
_, err = io.Copy(dst, body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Unable to save file %s: %w", filepath, err)
|
|
|
|
|
}
|
2026-04-01 21:23:28 +00:00
|
|
|
log.Info().Str("filepath", filepath).Int("collection", int(collection)).Msg("Saved image file content to collection")
|
2026-02-08 04:36:12 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
2026-04-01 21:23:28 +00:00
|
|
|
func ImageFileToWriter(collection Collection, uid uuid.UUID, w http.ResponseWriter) {
|
2026-04-16 19:49:18 +00:00
|
|
|
image_path := fileContentPathUUID(collection, uid)
|
2026-02-08 04:36:12 +00:00
|
|
|
writeFileContent(w, image_path)
|
|
|
|
|
}
|