2025-12-16 16:37:53 +00:00
|
|
|
package userfile
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2026-01-21 18:26:48 +00:00
|
|
|
"net/http"
|
2025-12-16 16:37:53 +00:00
|
|
|
"os"
|
|
|
|
|
|
2026-01-07 16:07:51 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/config"
|
2025-12-16 16:37:53 +00:00
|
|
|
"github.com/google/uuid"
|
2026-01-16 14:52:11 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2025-12-16 16:37:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func AudioFileContentPathRaw(audioUUID string) string {
|
2026-01-08 15:00:30 +00:00
|
|
|
return fmt.Sprintf("%s/%s.m4a", config.FilesDirectoryUser, audioUUID)
|
2025-12-16 16:37:53 +00:00
|
|
|
}
|
|
|
|
|
func AudioFileContentPathMp3(audioUUID string) string {
|
2026-01-08 15:00:30 +00:00
|
|
|
return fmt.Sprintf("%s/%s.mp3", config.FilesDirectoryUser, audioUUID)
|
2025-12-16 16:37:53 +00:00
|
|
|
}
|
|
|
|
|
func AudioFileContentPathNormalized(audioUUID string) string {
|
2026-01-08 15:00:30 +00:00
|
|
|
return fmt.Sprintf("%s/%s-normalized.m4a", config.FilesDirectoryUser, audioUUID)
|
2025-12-16 16:37:53 +00:00
|
|
|
}
|
|
|
|
|
func AudioFileContentPathOgg(audioUUID string) string {
|
2026-01-08 15:00:30 +00:00
|
|
|
return fmt.Sprintf("%s/%s.ogg", config.FilesDirectoryUser, audioUUID)
|
2025-12-16 16:37:53 +00:00
|
|
|
}
|
|
|
|
|
func AudioFileContentWrite(audioUUID uuid.UUID, body io.Reader) error {
|
|
|
|
|
// Create file in configured directory
|
|
|
|
|
filepath := AudioFileContentPathRaw(audioUUID.String())
|
|
|
|
|
dst, err := os.Create(filepath)
|
|
|
|
|
if err != nil {
|
2026-01-16 14:52:11 +00:00
|
|
|
log.Error().Err(err).Str("filepath", filepath).Msg("Failed to create audio file")
|
2025-12-16 16:37:53 +00:00
|
|
|
return fmt.Errorf("Failed to create audio file at %s: %v", 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 to create audio file at %s: %v", filepath, err)
|
|
|
|
|
}
|
2026-01-16 14:52:11 +00:00
|
|
|
log.Info().Str("filepath", filepath).Msg("Save audio file content")
|
2025-12-16 16:37:53 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
2026-01-22 03:27:32 +00:00
|
|
|
func ImageFileContentPathRawUser(uid string) string {
|
|
|
|
|
return imageFileContentPath(config.FilesDirectoryUser, uid, "raw")
|
|
|
|
|
}
|
|
|
|
|
func imageFileContentPathLogoPng(uid string) string {
|
|
|
|
|
return imageFileContentPath(config.FilesDirectoryLogo, uid, "png")
|
|
|
|
|
}
|
|
|
|
|
func imageFileContentPath(dir string, uid string, ext string) string {
|
|
|
|
|
return fmt.Sprintf("%s/%s.%s", dir, uid, ext)
|
2025-12-18 03:36:52 -07:00
|
|
|
}
|
|
|
|
|
func ImageFileContentWrite(uid uuid.UUID, body io.Reader) error {
|
2026-01-22 03:27:32 +00:00
|
|
|
filepath := ImageFileContentPathRawUser(uid.String())
|
2025-12-18 03:36:52 -07: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)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-01-22 03:27:32 +00:00
|
|
|
func ImageFileContentWriteLogo(w http.ResponseWriter, uid uuid.UUID) {
|
|
|
|
|
image_path := imageFileContentPathLogoPng(uid.String())
|
|
|
|
|
writeFileContent(w, image_path)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-08 15:00:30 +00:00
|
|
|
func PublicImageFileContentWrite(uid uuid.UUID, body io.Reader) error {
|
|
|
|
|
// Create file in configured directory
|
|
|
|
|
filepath := PublicImageFileContentPathRaw(uid.String())
|
|
|
|
|
dst, err := os.Create(filepath)
|
|
|
|
|
if err != nil {
|
2026-01-16 14:52:11 +00:00
|
|
|
log.Error().Err(err).Str("filepath", filepath).Msg("Failed to create public image file")
|
2026-01-08 15:00:30 +00:00
|
|
|
return fmt.Errorf("Failed to create public image file at %s: %v", 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 to create audio file at %s: %v", filepath, err)
|
|
|
|
|
}
|
2026-01-16 14:52:11 +00:00
|
|
|
log.Info().Str("filepath", filepath).Msg("Saved public report image file content")
|
2026-01-08 15:00:30 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
2026-01-21 18:26:48 +00:00
|
|
|
|
2026-01-08 15:00:30 +00:00
|
|
|
func PublicImageFileContentPathRaw(uid string) string {
|
|
|
|
|
return fmt.Sprintf("%s/%s.raw", config.FilesDirectoryPublic, uid)
|
|
|
|
|
}
|
2026-01-21 18:26:48 +00:00
|
|
|
|
|
|
|
|
func PublicImageFileToResponse(w http.ResponseWriter, uid string) {
|
|
|
|
|
image_path := PublicImageFileContentPathRaw(uid)
|
2026-01-22 03:27:32 +00:00
|
|
|
writeFileContent(w, image_path)
|
|
|
|
|
}
|
2026-01-21 18:26:48 +00:00
|
|
|
|
2026-01-22 03:27:32 +00:00
|
|
|
func writeFileContent(w http.ResponseWriter, image_path string) {
|
2026-01-21 18:26:48 +00:00
|
|
|
// Open the file
|
|
|
|
|
file, err := os.Open(image_path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
http.Error(w, "Image not found", http.StatusNotFound)
|
|
|
|
|
} else {
|
|
|
|
|
http.Error(w, "Failed to retrieve image", http.StatusInternalServerError)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
// Get file info for Content-Length header
|
|
|
|
|
fileInfo, err := file.Stat()
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, "Failed to get image information", http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set appropriate headers
|
|
|
|
|
w.Header().Set("Content-Type", "image/png")
|
|
|
|
|
w.Header().Set("Content-Length", fmt.Sprintf("%d", fileInfo.Size()))
|
|
|
|
|
|
|
|
|
|
// Copy file contents to response writer
|
|
|
|
|
_, err = io.Copy(w, file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// Note: At this point, we've already started writing the response,
|
|
|
|
|
// so we can't change the status code anymore. The best we can do
|
|
|
|
|
// is log the error and abandon the connection.
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|