示例#1
0
func ControllerUpdateAsset(w http.ResponseWriter, r *http.Request) {
	auth.CheckAuthCookie(w, r)
	vars := mux.Vars(r)
	id, _ := strconv.ParseInt(vars["id"], 10, 32)

	if r.Method == "POST" {
		err := r.ParseForm()
		if err != nil {
			// ERROR: Unable to read form data.
			log.Print("ERROR: Bad form data\n")
			http.Redirect(w, r, "/", 302)
		} else {
			asset := new(Asset)
			err := formDecoder.Decode(asset, r.PostForm)
			if err == nil {
				asset.Id = &id
				_ = UpdateAsset(asset)
			} else {
				fmt.Printf("%s\n", r.PostForm)
				fmt.Printf("%s\n", err)
			}

			http.Redirect(w, r, "/assets", 302)
		}
	} else {
		http.Redirect(w, r, "/assets", 302)
	}
}
示例#2
0
func ControllerDestroyAsset(w http.ResponseWriter, r *http.Request) {
	auth.CheckAuthCookie(w, r)
	vars := mux.Vars(r)
	id, _ := strconv.ParseInt(vars["id"], 10, 32)

	_ = DestroyAsset(id)
	http.Redirect(w, r, "/assets", 302)
}
示例#3
0
func ControllerCreateAsset(w http.ResponseWriter, r *http.Request) {
	auth.CheckAuthCookie(w, r)

	if r.Method == "POST" {
		err := r.ParseForm()
		if err != nil {
			// ERROR: Unable to read form data.
			log.Print("ERROR: Bad form data\n")
			http.Redirect(w, r, "/", 302)
		} else {
			// err := formDecoder.Decode(asset, r.PostForm)
			// if err == nil {
			_, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
			if err == nil {
				mr := multipart.NewReader(r.Body, params["boundary"])
				form, err := mr.ReadForm(1024)
				if err == nil {
					asset := new(Asset)
					asset.Name = strings.Join(form.Value["name"], "")
					if strings.Join(form.Value["isimage"], "") == "on" {
						asset.IsImage = true
					} else {
						asset.IsImage = false
					}
					if err == nil {
						if len(form.File["file"]) == 1 {
							fileName := form.File["file"][0].Filename
							destFileName := os.Getenv("ASSET_REPO_UPLOAD_DIR") + "/" + url.QueryEscape(fileName)
							asset.FileName = destFileName
							asset.URL = os.Getenv("ASSET_REPO_BASE_URL") + "/" + fileName
							// os.Copy(form.File["file"][0].tmpfile, destFileName)
							tmpFile, err := form.File["file"][0].Open()
							if err == nil {
								if err == nil {
									data, err := ioutil.ReadAll(tmpFile)
									if err == nil {
										err = ioutil.WriteFile(destFileName, data, 0660)
										if err == nil {
											err = CreateAsset(asset)
										}
										tmpFile.Close()
									}
								}
							}
						}

						form.RemoveAll()
					}
				}
			} else {
				log.Print("ERROR: Bad form data\n")
			}
		}
	}

	http.Redirect(w, r, "/assets", 302)
}
示例#4
0
func ControllerNewAsset(w http.ResponseWriter, r *http.Request) {
	auth.CheckAuthCookie(w, r)

	tmpl, err := template.ParseFiles(templatePath + "/assets/new.html")
	if err != nil {
		log.Fatal("Template missing: " + templatePath + "/assets/new.html")
	}

	err = tmpl.Execute(w, nil)
	if err != nil {
		log.Fatal("Unable to write new template to response.\n")
	}
}
示例#5
0
func ControllerShowIndex(w http.ResponseWriter, r *http.Request) {
	auth.CheckAuthCookie(w, r)
	assets := LoadStoredAssets()

	tmpl, err := template.ParseFiles(templatePath + "/assets/index.html")
	if err != nil {
		log.Fatal("Template missing: " + templatePath + "/assets/index.html")
	}

	err = tmpl.Execute(w, map[string]interface{}{"Assets": assets})
	if err != nil {
		log.Fatal("Unable to write index template to response.\n")
	}
}
示例#6
0
func ControllerEditAsset(w http.ResponseWriter, r *http.Request) {
	auth.CheckAuthCookie(w, r)
	vars := mux.Vars(r)
	id, _ := strconv.ParseInt(vars["id"], 10, 32)
	asset := FindAsset(id)
	if asset.Id == nil {
		// ERROR asset not found or DB error
		http.Redirect(w, r, "/assets", 302)
	}

	tmpl, err := template.ParseFiles(templatePath + "/assets/edit.html")
	if err != nil {
		log.Fatal("Template missing: " + templatePath + "/assets/edit.html")
	}

	err = tmpl.Execute(w, asset)
	if err != nil {
		log.Fatal("Unable to write edit template to response.\n")
	}
}