Example #1
0
func (u *Uploader) TryRequest(r *http.Request, w http.ResponseWriter) (status bool, err error, errCode int) {
	if !u.conf.UploaderEnable {
		return
	}
	token := r.URL.Query().Get(u.conf.UploaderParamName)
	if token == "" {
		return
	}

	aelog.Debugf("Uploader: token found: %s\n", token)

	status = true

	write := func(token string, params interface{}, err error) {
		var resp *http.Response
		if err != nil {
			resp, err = u.ctrl.SetStatusErr(token, err)
		} else {
			resp, err = u.ctrl.SetStatus(token, params)
		}
		if err != nil {
			errCode = 500
			return
		}
		w.Header().Set("Content-Type", "application/x-javascript")
		w.WriteHeader(resp.StatusCode)
		io.Copy(w, resp.Body)
		err = nil
	}

	files, err := u.ctrl.GetParams(token)
	if err != nil {
		if err == ErrTokenNotFound {
			aelog.Debugln("Uploader: ctrl return not found")
			errCode = 404
		} else {
			errCode = 500
			aelog.Debugf("Uploader: ctrl return error: %v\n", err)
			write(token, nil, err)
		}
		return
	}

	err = files.Upload(u.conf, u.stor, r, w)

	if err != nil {
		aelog.Debugf("Uploader: files.Upload has err: %v\n", err)
		errCode = 500
		write(token, nil, err)
		return
	}
	write(token, files, nil)
	aelog.Debugln("Uploader: files.Upload done")
	return
}
Example #2
0
func (c *Container) Init(s *Storage) (err error) {
	aelog.Debugln("Init container", c.Id)
	c.m = new(sync.Mutex)
	c.s = s
	// open file
	c.f, err = os.OpenFile(c.fileName(), os.O_RDWR|os.O_CREATE, 0666)
	if err != nil {
		return
	}
	c.holeIndex = new(HoleIndex)
	c.holeIndex.Init()
	c.restore()
	c.ch = true
	// build holeIndex
	/*var last, next Space
	next = nil
	last = c.last
	for last != nil && c.last != nil {
		if last.IsFree() {
			c.holeIndex.Add(last.(*Hole))
			aelog.Debug("init hole")
		} else {
			f := last.(*File)
			f.Init(c)
			c.s.Index.Add(f)
		}
		if next != nil {
			last.SetNext(next)
		}
		next = last
		last = last.Prev()
	}
	*/

	// create
	if !c.Created {
		aelog.Debugln("Create conatiner", c.Id)
		if err = c.create(); err == nil {
			c.Created = true
		}
		err = c.Dump()
	}

	return
}
Example #3
0
func (i *Image) optimize(dst string) {
	if i.Type != "png" {
		return
	}
	command := "pngquant"
	if _, err := exec.LookPath(command); err != nil {
		aelog.Warnln("Optimize image: Command", command, "not found:", err)
		return
	}

	cmd := exec.Command(command, dst, "--force", "--output", dst)
	if res, err := cmd.CombinedOutput(); err != nil {
		aelog.Warnf("Optimize image: %s return error: %v (%s)", command, err, string(res))
	}
	aelog.Debugln("Optimize image:", dst, "success!")
}