Finish green pool report submission
Also start the pattern of breaking out pool pages together in their own file. I think its easier to read this way.
This commit is contained in:
parent
9680fb6a68
commit
01ed2d6086
31 changed files with 5925 additions and 375 deletions
64
public-report/photo-upload.go
Normal file
64
public-report/photo-upload.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
package publicreport
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/userfile"
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type PhotoUpload struct {
|
||||
Filename string
|
||||
Size int64
|
||||
UUID uuid.UUID
|
||||
}
|
||||
|
||||
func extractPhotoUploads(r *http.Request) (uploads []PhotoUpload, err error) {
|
||||
uploads = make([]PhotoUpload, 0)
|
||||
for _, fheaders := range r.MultipartForm.File {
|
||||
for _, headers := range fheaders {
|
||||
file, err := headers.Open()
|
||||
|
||||
if err != nil {
|
||||
return uploads, fmt.Errorf("Failed to open header: %v", err)
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
buff := make([]byte, 512)
|
||||
file.Read(buff)
|
||||
|
||||
file.Seek(0, 0)
|
||||
contentType := http.DetectContentType(buff)
|
||||
var sizeBuff bytes.Buffer
|
||||
fileSize, err := sizeBuff.ReadFrom(file)
|
||||
if err != nil {
|
||||
return uploads, fmt.Errorf("Failed to read file: %v", err)
|
||||
}
|
||||
file.Seek(0, 0)
|
||||
contentBuf := bytes.NewBuffer(nil)
|
||||
if _, err := io.Copy(contentBuf, file); err != nil {
|
||||
return uploads, fmt.Errorf("Failed to save file: %v", err)
|
||||
}
|
||||
log.Info().Int64("size", fileSize).Str("filename", headers.Filename).Str("content-type", contentType).Msg("Got an uploaded file")
|
||||
u, err := uuid.NewUUID()
|
||||
if err != nil {
|
||||
return uploads, fmt.Errorf("Failed to create quick report photo uuid", err)
|
||||
}
|
||||
err = userfile.PublicImageFileContentWrite(u, file)
|
||||
if err != nil {
|
||||
return uploads, fmt.Errorf("Failed to write image file to disk: %v", err)
|
||||
}
|
||||
uploads = append(uploads, PhotoUpload{
|
||||
Size: fileSize,
|
||||
Filename: headers.Filename,
|
||||
UUID: u,
|
||||
})
|
||||
}
|
||||
}
|
||||
return uploads, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue