func createMedia(r *http.Request, mediaManager *media.Manager, db *sql.DB, sessionMessages *template.Messages, table_name string, owner_id int64) { filenames := mediaManager.Upload(r, sessionMessages, "media[1][image]", "media[2][image]", "media[3][image]") for key, filename := range filenames { media := entity.Media{} media.Fk = owner_id form_key := fmt.Sprintf("%d", key+1) media_data := make(map[string]string) media_data["filename"] = filename media_data["table"] = table_name media_data["type"] = "image" media_data["author"] = r.FormValue("media[" + form_key + "][author]") media_data["license"] = r.FormValue("media[" + form_key + "][license]") media_data["license_uri"] = r.FormValue("media[" + form_key + "][license_uri]") media.Populate(media_data) _, err := repository.InsertMedium(media, db) if err != nil { log.Printf("Inserting medium failed: %v\n", err) } } }
func ExecuteMediaDeletion(media entity.Media, mediaManager *media.Manager, db *sql.DB) (int, error) { count, err := repository.CountMediaUsageById(db, media.Filename) if err != nil { return 0, err } else if count < 1 { return 0, nil } // We only want to delete the file from storage if we are to delete the last link to it if count == 1 { filename, extension := splitFilename(media.Filename) mediaManager.Delete(filename, extension) } _, err = repository.DeleteMedium(db, media.Id) if err != nil { return 0, err } return 0, nil }