Massive rework of platform layer user/organization
The goal of this rework is to make it so I can pass around platform.User instead of a pair of models.Organization and models.User. This is useful for reason I kind of forget now, but it started with working on notifications and ballooned massively from there into refactoring a number of things that were bugging me. This also includes a tiny amount of work on server-side events (SSE). * background stuff lives inside the platform now, which I need for having it push updates through SSE * userfile now lives in the platform, under file, so other platform functions can safely use it * oauth is broken into pieces and inside platform because other stuff was calling it already, but badly. * notifications go into the platform as well
This commit is contained in:
parent
32dcc50c94
commit
44c4f17f32
85 changed files with 1492 additions and 1384 deletions
100
platform/file/base.go
Normal file
100
platform/file/base.go
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
"github.com/google/uuid"
|
||||
//"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func audioFileContentWrite(audioUUID uuid.UUID, body io.Reader) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var collectionToExtension map[Collection]string = map[Collection]string{
|
||||
CollectionAudioNormalized: "ogg",
|
||||
CollectionAudioRaw: "raw",
|
||||
CollectionAudioTranscoded: "ogg",
|
||||
CollectionCSV: "csv",
|
||||
CollectionLogo: "png",
|
||||
CollectionPublicImage: "img",
|
||||
CollectionImageRaw: "raw",
|
||||
}
|
||||
var collectionToSubdir map[Collection]string = map[Collection]string{
|
||||
CollectionAudioNormalized: "audio-normalized",
|
||||
CollectionAudioRaw: "audio-raw",
|
||||
CollectionAudioTranscoded: "audio-transcoded",
|
||||
CollectionCSV: "csv",
|
||||
CollectionLogo: "logo",
|
||||
CollectionPublicImage: "public-image",
|
||||
CollectionImageRaw: "image-raw",
|
||||
}
|
||||
|
||||
func ContentPath(collection Collection, uid uuid.UUID) string {
|
||||
return fileContentPath(collection, uid)
|
||||
}
|
||||
func fileContentPath(collection Collection, uid uuid.UUID) string {
|
||||
subdir, ok := collectionToSubdir[collection]
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("No subdir for collection %d", int(collection)))
|
||||
}
|
||||
extension, ok := collectionToExtension[collection]
|
||||
return fmt.Sprintf("%s/%s/%s.%s", config.FilesDirectory, subdir, uid.String(), extension)
|
||||
}
|
||||
|
||||
/*
|
||||
func fileContentWrite(body io.Reader, subdir string, uid uuid.UUID, extension string) error {
|
||||
// Create file in configured directory
|
||||
filepath := fileContentPath(subdir, uid, extension)
|
||||
dst, err := os.Create(filepath)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("filepath", filepath).Msg("Failed to create file")
|
||||
return fmt.Errorf("Failed to create 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 content of %s: %v", filepath, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
func writeFileContent(w http.ResponseWriter, image_path string) {
|
||||
// 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
|
||||
}
|
||||
}
|
||||
13
platform/file/enum.go
Normal file
13
platform/file/enum.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package file
|
||||
|
||||
type Collection int
|
||||
|
||||
const (
|
||||
CollectionAudioRaw Collection = iota
|
||||
CollectionAudioNormalized
|
||||
CollectionAudioTranscoded
|
||||
CollectionCSV
|
||||
CollectionImageRaw
|
||||
CollectionLogo
|
||||
CollectionPublicImage
|
||||
)
|
||||
60
platform/file/image.go
Normal file
60
platform/file/image.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func ImageFileContentWrite(uid uuid.UUID, body io.Reader) error {
|
||||
filepath := fileContentPath(CollectionImageRaw, uid)
|
||||
|
||||
// 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 ImageFileContentWriteLogo(w http.ResponseWriter, uid uuid.UUID) {
|
||||
//image_path := imageFileContentPathLogoPng(uid.String())
|
||||
image_path := fileContentPath(CollectionLogo, uid)
|
||||
writeFileContent(w, image_path)
|
||||
}
|
||||
|
||||
func PublicImageFileContentWrite(uid uuid.UUID, body io.Reader) error {
|
||||
// Create file in configured directory
|
||||
//filepath := PublicImageFileContentPathRaw(uid.String())
|
||||
filepath := fileContentPath(CollectionPublicImage, uid)
|
||||
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 PublicImageFileToResponse(w http.ResponseWriter, uid uuid.UUID) {
|
||||
//image_path := PublicImageFileContentPathRaw(uid)
|
||||
image_path := fileContentPath(CollectionPublicImage, uid)
|
||||
writeFileContent(w, image_path)
|
||||
}
|
||||
76
platform/file/upload.go
Normal file
76
platform/file/upload.go
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
package file
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type FileUpload struct {
|
||||
ContentType string
|
||||
Name string
|
||||
SizeBytes int
|
||||
UUID uuid.UUID
|
||||
}
|
||||
|
||||
func SaveFileUpload(r *http.Request, name string, collection Collection) ([]FileUpload, error) {
|
||||
results := make([]FileUpload, 0)
|
||||
for n, fheaders := range r.MultipartForm.File {
|
||||
log.Debug().Str("n", n).Msg("looking at header")
|
||||
if n != name {
|
||||
continue
|
||||
}
|
||||
for _, headers := range fheaders {
|
||||
f, err := saveFileUpload(headers, collection)
|
||||
if err != nil {
|
||||
return results, fmt.Errorf("Failed to extract photo upload: %w", err)
|
||||
}
|
||||
results = append(results, f)
|
||||
}
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
func saveFileUploads(r *http.Request, collection Collection) ([]FileUpload, error) {
|
||||
results := make([]FileUpload, 0)
|
||||
for name, fheaders := range r.MultipartForm.File {
|
||||
for _, headers := range fheaders {
|
||||
upload, err := saveFileUpload(headers, collection)
|
||||
if err != nil {
|
||||
return results, fmt.Errorf("Failed to save upload '%s': %w", name, err)
|
||||
}
|
||||
results = append(results, upload)
|
||||
}
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
func saveFileUpload(headers *multipart.FileHeader, collection Collection) (upload FileUpload, err error) {
|
||||
file, err := headers.Open()
|
||||
if err != nil {
|
||||
return upload, fmt.Errorf("Failed to open header: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
file_bytes, err := io.ReadAll(file)
|
||||
content_type := http.DetectContentType(file_bytes)
|
||||
|
||||
u, err := uuid.NewUUID()
|
||||
if err != nil {
|
||||
return upload, fmt.Errorf("Failed to create uuid", err)
|
||||
}
|
||||
err = FileContentWrite(bytes.NewReader(file_bytes), collection, u)
|
||||
if err != nil {
|
||||
return upload, fmt.Errorf("Failed to write file to disk: %w", err)
|
||||
}
|
||||
log.Info().Int("size", len(file_bytes)).Str("uploaded_filename", headers.Filename).Str("content-type", content_type).Str("uuid", u.String()).Msg("Saved an uploaded file to disk")
|
||||
return FileUpload{
|
||||
ContentType: content_type,
|
||||
Name: headers.Filename,
|
||||
SizeBytes: len(file_bytes),
|
||||
UUID: u,
|
||||
}, nil
|
||||
}
|
||||
50
platform/file/userfile.go
Normal file
50
platform/file/userfile.go
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
//"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func CreateDirectories() error {
|
||||
for _, subdir := range collectionToSubdir {
|
||||
path := config.FilesDirectory + "/" + subdir
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
err = os.MkdirAll(path, 0750)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create userfile directory '%s': %w", path, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func FileContentWrite(body io.Reader, collection Collection, uid uuid.UUID) error {
|
||||
// Create file in configured directory
|
||||
filepath := fileContentPath(collection, uid)
|
||||
dst, err := os.Create(filepath)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("filepath", filepath).Msg("Failed to create upload file")
|
||||
return fmt.Errorf("Failed to create upload 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 copy file content to %s: %v", filepath, err)
|
||||
}
|
||||
log.Info().Str("filepath", filepath).Msg("Save upload file content")
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewFileReader(collection Collection, uid uuid.UUID) (io.Reader, error) {
|
||||
path := fileContentPath(collection, uid)
|
||||
return os.Open(path)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue