Exemplo n.º 1
0
func (api *ImgServerApi) imageHandler(w traffic.ResponseWriter, r *traffic.Request) {

	params := r.URL.Query()
	width := toInt(params.Get("width"), 0)
	height := toInt(params.Get("height"), 0)
	imagefile := params.Get("image")
	cacheKey := imagefile + string(width) + string(height)

	modTime := api.imageDir.ModTime(imagefile)
	if modTime == nil {
		api.imageCache.Remove(api.imageCache.FindKeys(imagefile))
		w.WriteHeader(http.StatusNotFound)
		return
	}

	if cachedImage := api.imageCache.Get(cacheKey, *modTime); cachedImage != nil {
		sendJpeg(w, cachedImage)
	} else {
		origImage, err := api.imageDir.ReadImage(imagefile)
		if err != nil {
			traffic.Logger().Print(err.Error())
			w.WriteHeader(http.StatusNotFound)
		} else {
			semaphore <- struct{}{}
			buffer := new(bytes.Buffer)
			sizedImage := resizeImage(origImage, width, height, api.faceDetection)
			err = jpeg.Encode(buffer, sizedImage, &jpeg.Options{95})
			<-semaphore
			if err != nil {
				traffic.Logger().Print(err.Error())
				w.WriteHeader(http.StatusInternalServerError)
			} else {
				data := make([]byte, buffer.Len())
				buffer.Read(data)
				api.imageCache.Put(cacheKey, data, *modTime)
				sendJpeg(w, data)
			}
		}
	}
}
Exemplo n.º 2
0
func (api *ImgServerApi) deleteHandler(w traffic.ResponseWriter, r *traffic.Request) {

	filename := r.URL.Query().Get("image")
	err := api.imageDir.DeleteFile(filename)
	api.imageCache.Remove(api.imageCache.FindKeys(filename))

	if err != nil {
		traffic.Logger().Print(err.Error())
		w.WriteHeader(http.StatusNotFound)
	} else {
		w.WriteHeader(http.StatusOK)
	}
}
Exemplo n.º 3
0
func (api *ImgServerApi) listHandler(w traffic.ResponseWriter, r *traffic.Request) {

	age, err := strconv.Atoi(r.Param("age"))
	if err != nil {
		age = 0
	}

	files, err := api.imageDir.ListFiles(time.Duration(age) * time.Second)
	if err != nil {
		traffic.Logger().Print(err.Error())
		w.WriteHeader(http.StatusInternalServerError)
	} else {
		w.WriteJSON(files)
	}
}
Exemplo n.º 4
0
func (api *ImgServerApi) uploadHandler(w traffic.ResponseWriter, r *traffic.Request) {

	filename := r.URL.Query().Get("image")
	uploadedImage, _, err := image.Decode(r.Body)

	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
	} else {
		img := drawOnWhite(uploadedImage.Bounds().Size(), image.Pt(0, 0), image.Pt(0, 0), uploadedImage)
		err = api.imageDir.WriteImage(filename, img, 100)
		if err != nil {
			traffic.Logger().Print(err.Error())
			w.WriteHeader(http.StatusInternalServerError)
		} else {
			w.WriteHeader(http.StatusOK)
		}
	}
}
Exemplo n.º 5
0
func (api *ImgServerApi) copyHandler(w traffic.ResponseWriter, r *traffic.Request) {

	src := r.URL.Query().Get("src")
	dst := r.URL.Query().Get("dst")

	data, err := api.imageDir.ReadFile(src)
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	err = api.imageDir.WriteFile(dst, data)
	if err != nil {
		traffic.Logger().Print(err.Error())
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	w.WriteHeader(http.StatusOK)
}
Exemplo n.º 6
0
func rootHandler(w traffic.ResponseWriter, r *traffic.Request) {
	traffic.Logger().Print("Hello")
	w.WriteText("Hello World\n")
}