This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/routes/routes.go

139 lines
3.9 KiB
Go
Raw Normal View History

2021-01-03 22:31:16 +00:00
package routes
import (
2021-01-12 04:48:32 +00:00
"encoding/json"
2021-01-03 22:31:16 +00:00
"net/http"
2021-01-12 04:48:32 +00:00
"reichard.io/imagini/internal/context"
2021-01-03 22:31:16 +00:00
)
2021-01-12 04:48:32 +00:00
type ImaginiContext struct {
*context.ImaginiContext
2021-01-03 22:31:16 +00:00
}
2021-01-12 04:48:32 +00:00
func RegisterRoutes(cctx *context.ImaginiContext) {
ctx := &ImaginiContext{cctx}
http.HandleFunc("/MediaItems", ctx.mediaItemsHandler)
http.HandleFunc("/Upload", ctx.uploadHandler)
http.HandleFunc("/Albums", ctx.albumsHandler)
http.HandleFunc("/Logout", ctx.logoutHandler)
http.HandleFunc("/Login", ctx.loginHandler)
http.HandleFunc("/Users", ctx.usersHandler)
http.HandleFunc("/Tags", ctx.tagsHandler)
http.HandleFunc("/Info", ctx.infoHandler)
http.HandleFunc("/Me", ctx.meHandler)
}
// https://stackoverflow.com/a/59764037
func JSONError(w http.ResponseWriter, err string, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)
json.NewEncoder(w).Encode(map[string]interface{}{"error": err})
}
func JSONSuccess(w http.ResponseWriter, msg string, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)
json.NewEncoder(w).Encode(map[string]interface{}{"success": msg})
}
// METHOD:
// switch r.Method {
// case http.MethodGet:
// // Serve the resource.
// case http.MethodPost:
// // Create a new record.
// case http.MethodPut:
// // Update an existing record.
// case http.MethodDelete:
// // Remove the record.
// default:
// // Give an error message.
// }
2021-01-06 19:36:09 +00:00
2021-01-10 00:44:02 +00:00
2021-01-12 04:48:32 +00:00
// commonMiddleware := []Middleware{
// logMiddleware,
// authMiddleware,
// }
// http.Handle("/Users", MultipleMiddleware(usersHandler, commonMiddleware...))
// http.Handle("/Uploads/", MultipleMiddleware(uploadsHandler, commonMiddleware...))
2021-01-10 00:44:02 +00:00
2021-01-12 04:48:32 +00:00
// // http.HandleFunc("/uploads/", uploadsHandler())
// http.Handle("/Uploads/", func(next http.Handler) http.Handler {
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// _, ok := ValidateUserToken(r)
// if ok {
// next.ServeHTTP(w, r)
// } else {
// w.WriteHeader(http.StatusUnauthorized)
// }
// })
// }(http.StripPrefix("/Uploads/", tusHandler)))
2021-01-10 00:44:02 +00:00
// Filter Example:
// query := r.URL.Query()
// filters, present := query["filters"]
// func uploadsHandler() http.Handler {
// store := filestore.FileStore{
// Path: "./Uploads",
// }
// composer := tusd.NewStoreComposer()
// store.UseIn(composer)
//
// handler, err := tusd.NewHandler(tusd.Config{
// BasePath: "/uploads/",
// StoreComposer: composer,
// NotifyCompleteUploads: true,
// })
//
// if err != nil {
// panic(fmt.Errorf("Unable to create handler: %s", err))
// }
//
// go func() {
// for {
// event := <-handler.CompleteUploads
// fmt.Printf("Upload %s finished\n", event.Upload.ID)
// }
// }()
//
// // return func(w http.ResponseWriter, r *http.Request) {
// // http.StripPrefix("/Uploads/", handler).ServeHTTP(w, r)
// // };
//
// return http.StripPrefix("/Uploads/", handler)
// }
2021-01-06 19:36:09 +00:00
2021-01-08 02:45:59 +00:00
// func processMedia() {
// var mi db.MediaItem
//
2021-01-06 19:36:09 +00:00
// TODO:
// - Derive Magic -> mediaType
// - Create Thumbnail
// - Pull EXIF
// - EXIFDate
// - Latitude
// - Longitude
// - TensorFlow Classification
// - https://outcrawl.com/image-recognition-api-go-tensorflow
// - Update Tags / MediaTags Table
// - Save Image
// - Update MediaItems Table
// import "github.com/disintegration/imaging"
//
// img, err := imaging.Open("original.jpg", imaging.AutoOrientation(true))
// if err != nil {
// return nil, err
// }
//
// img = imaging.Fit(img, 240, 160, imaging.Lanczos)
// err = imaging.Save(img, "thumbnail.jpg")
2021-01-08 02:45:59 +00:00
// }