func (PageResource) BeforeDelete(res kit.Resource, obj kit.Model, user kit.User) apperror.Error { // Note: other relationship deletion handling happens in models BeforeDelete() hook. // Files have to be deleted here, since they require the fileService. // Delete files. fileService := res.Registry().FileService() m2m, err := res.Backend().M2M(obj, "Files") if err != nil { return err } files, err := m2m.All() if err != nil { return err } // Delete m2m relation. if err := m2m.Clear(); err != nil { return err } for _, file := range files { if err := fileService.Delete(file.(kit.File), user); err != nil { return err } } return nil }
// ApiFindOne verifies the session and returns user and profile in meta if valid. func (SessionResourceHooks) ApiFindOne(res kit.Resource, rawId string, r kit.Request) kit.Response { if rawId == "" { return kit.NewErrorResponse("empty_token", "Empty token") } userService := res.Registry().UserService() user, session, err := userService.VerifySession(rawId) if err != nil { return kit.NewErrorResponse(err) } meta := make(map[string]interface{}) if user != nil { userData, err := res.Backend().ModelToMap(user, true, false) if err != nil { return kit.NewErrorResponse("marshal_error", err) } meta["user"] = userData if user.GetProfile() != nil { profileData, err := res.Backend().ModelToMap(user.GetProfile(), true, false) if err != nil { return kit.NewErrorResponse("marshal_error", err) } meta["profile"] = profileData } } return &kit.AppResponse{ Data: session, Meta: meta, } }
func (_ FilesResource) ApiCreate(res kit.Resource, obj kit.Model, r kit.Request) kit.Response { // Verify that tmp path is set either in metadata or on model. file := obj.(kit.File) if file.GetTmpPath() == "" { file.SetTmpPath(r.GetMeta().String("file")) } filePath := file.GetTmpPath() if filePath == "" { return kit.NewErrorResponse("no_tmp_path", "A tmp path must be set when creating a file", true) } tmpPath := getTmpPath(res) if !strings.HasPrefix(filePath, tmpPath) && filePath[0] != '/' { filePath = tmpPath + string(os.PathSeparator) + filePath file.SetTmpPath(filePath) } // Build the file, save it to backend and persist it to the db. err := res.Registry().FileService().BuildFile(file, r.GetUser(), true, true) if err != nil { kit.NewErrorResponse(err) } return &kit.AppResponse{ Data: file, } }
// Creating a session is equivalent to logging in. func (hooks SessionResourceHooks) ApiCreate(res kit.Resource, obj kit.Model, r kit.Request) kit.Response { userService := res.Registry().UserService() meta := r.GetMeta() isAnonymous, _ := meta.Bool("anonymous") // Find user. userIdentifier := meta.String("user") adaptor := meta.String("adaptor") data, _ := meta.Map("authData") var user kit.User if !isAnonymous { if adaptor == "" { return kit.NewErrorResponse("adaptor_missing", "Expected 'adaptor' in metadata.", true) } if data == nil { kit.NewErrorResponse("no_or_invalid_auth_data", "Expected 'authData' dictionary in metadata.") } var err apperror.Error user, err = userService.AuthenticateUser(userIdentifier, adaptor, data) if err != nil { return kit.NewErrorResponse(err) } } session, err := userService.StartSession(user, r.GetFrontend()) if err != nil { return kit.NewErrorResponse(err) } responseMeta := make(map[string]interface{}) if !isAnonymous { userData, err := res.Backend().ModelToMap(user, true, false) if err != nil { return kit.NewErrorResponse("marshal_error", err) } responseMeta["user"] = userData if user.GetProfile() != nil { profileData, err := res.Backend().ModelToMap(user.GetProfile(), true, false) if err != nil { return kit.NewErrorResponse("marshal_error", err) } responseMeta["profile"] = profileData } } return &kit.AppResponse{ Data: session, Meta: responseMeta, } }
func getTmpPath(res kit.Resource) string { c := res.Registry().Config() tmpPath := c.UPath("tmpUploadDir") if tmpPath == "" { tmpPath = c.TmpDir() if tmpPath == "" { panic("config.TmpDir() empty") } tmpPath = path.Join(tmpPath, "uploads") } return tmpPath }
func (hooks UserResourceHooks) ApiCreate(res kit.Resource, obj kit.Model, r kit.Request) kit.Response { meta := r.GetMeta() adaptor := meta.String("adaptor") if adaptor == "" { return kit.NewErrorResponse("adaptor_missing", "Expected 'adaptor' in metadata.", true) } rawData, ok := meta.Get("authData") if !ok { return kit.NewErrorResponse("auth_data_missing", "Expected 'authData' in metadata.", true) } data, ok := rawData.(map[string]interface{}) if !ok { return kit.NewErrorResponse("invalid_auth_data", "Invalid auth data: expected dictionary", true) } user := obj.(kit.User) service := res.Registry().UserService() // If a profile model was registered, and profile data is in meta, // create the profile model. if profiles := service.ProfileResource(); profiles != nil { profile := profiles.CreateModel().(kit.UserProfile) if rawData, ok := meta.Get("profile"); ok { if data, ok := rawData.(map[string]interface{}); ok { // Profile data present in meta. // Update profile with data. if err := res.ModelInfo().UpdateModelFromData(profile, data); err != nil { return kit.NewErrorResponse("invalid_profile_data", "Invalid profile data.", err, true) } } } user.SetProfile(profile) } if err := service.CreateUser(user, adaptor, data); err != nil { return kit.NewErrorResponse(err) } return &kit.AppResponse{ Data: user, } }
func (a *App) RegisterResource(res kit.Resource) { if res.Backend() == nil { if a.registry.DefaultBackend() == nil { a.Logger().Panic("Registering resource without backend, but no default backend set.") } // Set backend. res.SetBackend(a.registry.DefaultBackend()) } if res.Registry() == nil { res.SetRegistry(a.registry) } res.SetDebug(a.Debug()) // Allow a resource to register custom http routes and methods. if res.Hooks() != nil { // Handle http routes. if resRoutes, ok := res.Hooks().(resources.ApiHttpRoutes); ok { for _, route := range resRoutes.HttpRoutes(res) { a.RegisterHttpHandler(route.Method(), route.Route(), route.Handler()) } } // Handle methods. if resMethods, ok := res.Hooks().(resources.MethodsHook); ok { for _, method := range resMethods.Methods(res) { a.RegisterMethod(method) } } } a.registry.AddResource(res) }
func (hooks FilesResource) HttpRoutes(res kit.Resource) []kit.HttpRoute { maxRunning := res.Registry().Config().UInt("files.thumbGenerator.maxRunning", 10) maxPerIPPerMinute := res.Registry().Config().UInt("files.thumbGenerator.maxPerIPPerMinute", 100) maxQueueSize := res.Registry().Config().UInt("files.thumbGenerator.maxQueueSize", 100) hooks.thumbnailRateLimiter = newRateLimiter(maxRunning, maxPerIPPerMinute, maxQueueSize) routes := make([]kit.HttpRoute, 0) // Upload route. uploadOptionsHandler := func(registry kit.Registry, r kit.Request) (kit.Response, bool) { header := r.GetHttpResponseWriter().Header() allowedOrigins := registry.Config().UString("fileHandler.allowedOrigins", "*") header.Set("Access-Control-Allow-Origin", allowedOrigins) header.Set("Access-Control-Allow-Methods", "OPTIONS, POST") allowedHeaders := registry.Config().UString("accessControl.allowedHeaders") if allowedHeaders == "" { allowedHeaders = "Authentication, Content-Type, Content-Range, Content-Disposition" } else { allowedHeaders += ", Authentication, Content-Type, Content-Range, Content-Disposition" } header.Set("Access-Control-Allow-Headers", allowedHeaders) return &kit.AppResponse{ HttpStatus: 200, RawData: []byte{}, }, true } uploadOptionsRoute := kit.NewHttpRoute("/api/file-upload", "OPTIONS", uploadOptionsHandler) routes = append(routes, uploadOptionsRoute) tmpPath := getTmpPath(res) if tmpPath == "" { panic("Empty tmp path") } uploadHandler := func(registry kit.Registry, r kit.Request) (kit.Response, bool) { if registry.Config().UBool("fileHandler.requiresAuth", false) { if r.GetUser() == nil { return kit.NewErrorResponse("permission_denied", ""), false } } var files []string var err apperror.Error if err == nil { files, err = handleUpload(registry, tmpPath, r.GetHttpRequest()) if err != nil { return kit.NewErrorResponse(err), false } } data := map[string]interface{}{ "data": files, } return &kit.AppResponse{Data: data}, false } uploadRoute := kit.NewHttpRoute("/api/file-upload", "POST", uploadHandler) routes = append(routes, uploadRoute) serveFileHandler := func(registry kit.Registry, r kit.Request) (kit.Response, bool) { file, err := registry.FileService().FindOne(r.GetContext().String("id")) if err != nil { return kit.NewErrorResponse(err), false } if file == nil { return &kit.AppResponse{ HttpStatus: 404, RawData: []byte("File not found"), }, false } reader, err := file.Reader() if err != nil { return kit.NewErrorResponse(err), false } defer reader.Close() w := r.GetHttpResponseWriter() err = serveFile(w, r.GetHttpRequest(), file.GetMime(), file.GetSize(), reader) reader.Close() if err != nil { registry.Logger().Errorf("Error while serving file %v(%v): %v", file.GetId(), file.GetBackendId(), err) } return nil, true } serveFileRoute := kit.NewHttpRoute("/files/:id/*rest", "GET", serveFileHandler) routes = append(routes, serveFileRoute) serveImageHandler := func(registry kit.Registry, r kit.Request) (kit.Response, bool) { file, err := registry.FileService().FindOne(r.GetContext().String("id")) if err != nil { return kit.NewErrorResponse(err), false } if file == nil { return &kit.AppResponse{ HttpStatus: 404, RawData: []byte("File not found"), }, false } if !file.GetIsImage() { return &kit.AppResponse{ Error: &apperror.Err{ Code: "file_is_no_image", Message: "The requested file is not an image", }, }, false } httpRequest := r.GetHttpRequest() query := httpRequest.URL.Query() rawWidth := query.Get("width") rawHeight := query.Get("height") var width, height int64 if rawWidth != "" { width, _ = strconv.ParseInt(rawWidth, 10, 64) } if rawHeight != "" { height, _ = strconv.ParseInt(rawHeight, 10, 64) } rawFilters := query.Get("filters") filters := strings.Split(rawFilters, ",") thumbDir := registry.Config().UString("thumbnailDir") if thumbDir == "" { thumbDir = tmpPath + string(os.PathSeparator) + "thumbnails" } ip := strings.Split(httpRequest.RemoteAddr, ":")[0] if ip == "" { ip = httpRequest.Header.Get("X-Forwarded-For") } reader, size, err := hooks.getImageReader(registry, thumbDir, file, width, height, filters, ip) if err != nil { return kit.NewErrorResponse(err), false } w := r.GetHttpResponseWriter() err = serveFile(w, httpRequest, file.GetMime(), size, reader) reader.Close() if err != nil { registry.Logger().Errorf("Error while serving image %v(%v): %v", file.GetId(), file.GetBackendId(), err) } return nil, true } serveImageRoute := kit.NewHttpRoute("/images/:id/*rest", "GET", serveImageHandler) routes = append(routes, serveImageRoute) return routes }