Пример #1
0
func imagePageHandler(w http.ResponseWriter, req *http.Request) {
	if req.Method == "GET" {
		vars := mux.Vars(req)
		id := vars["id"]
		if len(id) > fsys.ImgNameLength {
			if strings.Contains(id, ".") {
				id = strings.Split(id, ".")[0]
			}
			found, imgPath, imgID, title, tUp, ttlTime, ttlViews, errc, err := db.GetImage(id)

			/* Check whether the time to live limit
			 * has been reached (time of upload + allowed minutes)
			 */
			if ttlTime > 0 {
				ttlTimeT := time.Date(
					tUp.Year(),
					tUp.Month(),
					tUp.Day(),
					tUp.Hour(),
					tUp.Minute()+int(ttlTime),
					tUp.Second(),
					tUp.Nanosecond(),
					time.Local,
				)
				if time.Now().After(ttlTimeT) /*|| ttlViews == 0*/ {
					http.Redirect(w, req, "/error", 403)
					err := db.DeleteImage(imgID)
					if err != nil {
						return
						cio.PrintMessage(1, err.Error())
					}
					err = fsys.DeleteFile(imgPath)
					if err != nil {
						cio.PrintMessage(1, err.Error())
						return
					}
					return
				}
			}

			/* Decrease the views left for this image
			 * if the view count is not unlimited (unlimited = smaller than 0)
			 * Check whether the view limit has been
			 * reached.
			 */
			if ttlViews > -1 {
				if ttlViews == 0 {
					http.Redirect(w, req, "/error", 403)
					err := db.DeleteImage(imgID)
					if err != nil {
						cio.PrintMessage(1, err.Error())
					}
					err = fsys.DeleteFile(imgPath)
					if err != nil {
						cio.PrintMessage(1, err.Error())
					}
					return
				}
				err := db.UpdateImageViewCount(imgID)
				if err != nil {
					cio.PrintMessage(1, err.Error())
				}
				ttlViews--
			}

			if err != nil {
				cio.PrintMessage(1, err.Error())
				if errc == http.StatusInternalServerError {
					http.Error(w, err.Error(), errc)
					return
				} else if errc == http.StatusNotFound {
					errorHandler(w, req)
					return
				}
			}
			if found == true {
				img := Img{title, "/img/" + imgID}
				fp := path.Join("public", "img.html")
				tmpl, err := template.ParseFiles(fp)
				if err != nil {
					cio.PrintMessage(1, err.Error())
					http.Error(w, err.Error(), http.StatusInternalServerError)
					return
				}
				if err := tmpl.Execute(w, img); err != nil {
					cio.PrintMessage(1, err.Error())
					http.Error(w, err.Error(), http.StatusInternalServerError)
					return
				}
				cio.PrintMessage(0, imgPath+" rendered into img.html")
				return
			}
			errorHandler(w, req)
			return
		}
		errorHandler(w, req)
		return
	}
}
Пример #2
0
func imageHandler(w http.ResponseWriter, req *http.Request) {
	if req.Method == "GET" {
		vars := mux.Vars(req)
		id := vars["id"]
		if len(id) > fsys.ImgNameLength {
			if strings.Contains(id, ".") {
				id = strings.Split(id, ".")[0]
			}
			found, imgPath, imgID, _, tUp, ttlTime, _, errc, err := db.GetImage(id)

			/* Check whether the time to live limit
			 * has been reached (time of upload + allowed minutes)
			 */
			if ttlTime > 0 {
				ttlTimeT := time.Date(
					tUp.Year(),
					tUp.Month(),
					tUp.Day(),
					tUp.Hour(),
					tUp.Minute()+int(ttlTime),
					tUp.Second(),
					tUp.Nanosecond(),
					time.Local,
				)
				if time.Now().After(ttlTimeT) /*|| ttlViews == 0*/ {
					http.Redirect(w, req, "/error", 403)
					err := db.DeleteImage(imgID)
					if err != nil {
						cio.PrintMessage(1, err.Error())
						return
					}
					err = fsys.DeleteFile(imgPath)
					if err != nil {
						cio.PrintMessage(1, err.Error())
						return
					}
					return
				}
			}

			/* There is no check for the view count
				 * on direct calls for the image itself (instead
			   * of its page) to avoid the count decreasing
				 * when someone downloads the image
			*/

			if err != nil {
				cio.PrintMessage(1, (string(errc) + " " + err.Error()))
				if errc == http.StatusInternalServerError {
					http.Error(w, err.Error(), http.StatusInternalServerError)
					return
				} else if errc == http.StatusNotFound {
					errorHandler(w, req)
					return
				}
			}
			if found == true {
				http.ServeFile(w, req, imgPath)
				return
			}
			http.Error(w, errors.New("Image with ID "+id+" could not be found.").Error(), http.StatusNotFound)
			return
		}
		http.Error(w, errors.New(id+" is not a valid ID.").Error(), http.StatusNotFound)
		return
	}
}