示例#1
0
文件: http.go 项目: cheggaaa/Anteater
// Http handler for read-only server
func (s *Server) ReadOnly(w http.ResponseWriter, r *http.Request) {
	w.Header().Add("Server", cnst.SIGN)
	defer func() {
		if rec := recover(); rec != nil {
			s.Err(500, r, w)
			aelog.Warnf("Error on http request: %v", rec)
		}
		r.Body.Close()
	}()
	filename := Filename(r)
	if len(filename) == 0 {
		s.Err(404, r, w)
		return
	}

	switch r.Method {
	case "OPTIONS":
		w.Header().Set("Allow", "GET,HEAD")
		w.WriteHeader(http.StatusOK)
		return
	case "GET":
		s.Get(filename, w, r, true)
		return
	case "HEAD":
		s.Get(filename, w, r, false)
		return
	default:
		s.Err(501, r, w)
	}
}
示例#2
0
func (s *Storage) Dump() {
	s.m.Lock()
	defer s.m.Unlock()
	for _, c := range s.Containers {
		err := c.Dump()
		if err != nil {
			aelog.Warnf("Can't dump container %d: %v", c.Id, err)
		}
	}
}
示例#3
0
func (i *Index) get(name string) (f *File, ok bool) {
	parts := i.explode(name)
	var err error
	if f, err = i.Root.Get(parts, 0); err == nil {
		ok = true
		return
	}
	if err != ErrFileNotFound {
		aelog.Warnf("Error while get from index. %s: %v", name, err)
	}
	return
}
示例#4
0
func (i *Index) delete(name string) (f *File, ok bool) {
	parts := i.explode(name)
	var err error
	if f, err = i.Root.Delete(parts, 0); err == nil {
		ok = true
		atomic.AddInt64(&i.v, 1)
		atomic.AddInt64(&i.c, -1)
		return
	}
	if err != ErrFileNotFound {
		aelog.Warnf("Error while delete from index. %s: %v", name, err)
	}
	return
}
示例#5
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!")
}
示例#6
0
文件: http.go 项目: cheggaaa/Anteater
func (s *Server) Rename(name string, w http.ResponseWriter, r *http.Request) {
	_, ok := s.stor.Index.Get(name)
	if !ok {
		s.Err(http.StatusNotFound, r, w)
		return
	}
	newName := strings.Trim(r.Header.Get("X-Ae-Name"), "/")
	if newName == "" {
		s.Err(http.StatusBadRequest, r, w)
		return
	}

	forceString := strings.TrimSpace(r.Header.Get("X-Ae-Force"))
	force := false
	switch strings.ToLower(forceString) {
	case "1", "true":
		force = true
	}
	if _, ok = s.stor.Get(newName); ok {
		if !force {
			s.Err(http.StatusConflict, r, w)
			return
		} else {
			s.stor.Delete(newName)
		}
	}

	_, err := s.stor.Index.Rename(name, newName)
	switch err {
	case storage.ErrFileNotFound:
		s.Err(http.StatusNotFound, r, w)
		return
	case storage.ErrConflict:
		s.Err(http.StatusConflict, r, w)
		return
	default:
		if err != nil {
			aelog.Warnf("Can't rename file: %v", err)
			s.Err(http.StatusInternalServerError, r, w)
			return
		}
	}
	s.Get(newName, w, r, false)
	return
}
示例#7
0
文件: http.go 项目: cheggaaa/Anteater
// Http handler for read-write server
func (s *Server) ReadWrite(w http.ResponseWriter, r *http.Request) {
	w.Header().Add("Server", cnst.SIGN)
	defer func() {
		if rec := recover(); rec != nil {
			s.Err(500, r, w)
			aelog.Warnf("Error on http request: %v", rec)
		}
		r.Body.Close()
	}()
	filename := Filename(r)
	switch filename {
	case "":
		// check uploader
		isU, err, errCode := s.up.TryRequest(r, w)
		if isU {
			if err == nil && errCode > 0 {
				s.Err(errCode, r, w)
			}
			return
		}
		s.Err(404, r, w)
		return
	case s.conf.StatusHtml:
		s.Err(500, r, w)
		return
	case s.conf.StatusJson:
		s.StatsJson(w, r)
		return
	}

	m := r.Header.Get("X-Http-Method-Override")
	if m == "" {
		m = r.Method
	}
	sm := m

	du := s.downloadUrl(r)
	if du != "" {
		sm = "DOWNLOAD"
	}

	switch sm {
	case "OPTIONS":
		w.Header().Set("Allow", "GET,HEAD,POST,PUT,DELETE")
		w.WriteHeader(http.StatusOK)
		return
	case "GET":
		s.Get(filename, w, r, true)
		return
	case "HEAD":
		s.Get(filename, w, r, false)
		return
	case "POST":
		s.Save(filename, w, r)
		return
	case "PUT":
		s.Delete(filename, nil, nil)
		s.Save(filename, w, r)
		return
	case "DELETE":
		s.Delete(filename, w, r)
		return
	case "DOWNLOAD":
		if m == "PUT" {
			s.Delete(filename, nil, nil)
		}
		s.Download(filename, w, r)
		return
	case "COMMAND":
		s.Command(filename, w, r)
		return
	case "RENAME":
		s.Rename(filename, w, r)
		return
	default:
		s.Err(501, r, w)
	}
}