This required a schema change and actually dumps all existing photo data from the public reports page. That's probably fine since it's not deployed to any customers so all data is currently test data.
83 lines
2.7 KiB
Go
83 lines
2.7 KiB
Go
package userfile
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/config"
|
|
"github.com/google/uuid"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func AudioFileContentPathRaw(audioUUID string) string {
|
|
return fmt.Sprintf("%s/%s.m4a", config.FilesDirectoryUser, audioUUID)
|
|
}
|
|
func AudioFileContentPathMp3(audioUUID string) string {
|
|
return fmt.Sprintf("%s/%s.mp3", config.FilesDirectoryUser, audioUUID)
|
|
}
|
|
func AudioFileContentPathNormalized(audioUUID string) string {
|
|
return fmt.Sprintf("%s/%s-normalized.m4a", config.FilesDirectoryUser, audioUUID)
|
|
}
|
|
func AudioFileContentPathOgg(audioUUID string) string {
|
|
return fmt.Sprintf("%s/%s.ogg", config.FilesDirectoryUser, audioUUID)
|
|
}
|
|
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 {
|
|
log.Error().Err(err).Str("filepath", filepath).Msg("Failed to create audio file")
|
|
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)
|
|
}
|
|
log.Info().Str("filepath", filepath).Msg("Save audio file content")
|
|
return nil
|
|
}
|
|
func ImageFileContentPathRaw(uid string) string {
|
|
return fmt.Sprintf("%s/%s.raw", config.FilesDirectoryUser, uid)
|
|
}
|
|
func ImageFileContentWrite(uid uuid.UUID, body io.Reader) error {
|
|
filepath := ImageFileContentPathRaw(uid.String())
|
|
|
|
// 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
|
|
}
|
|
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 {
|
|
log.Error().Err(err).Str("filepath", filepath).Msg("Failed to create public image file")
|
|
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)
|
|
}
|
|
log.Info().Str("filepath", filepath).Msg("Saved public report image file content")
|
|
return nil
|
|
}
|
|
func PublicImageFileContentPathRaw(uid string) string {
|
|
return fmt.Sprintf("%s/%s.raw", config.FilesDirectoryPublic, uid)
|
|
}
|