Esempio n. 1
0
func fileUploadHandler(request *http.Request, resp http.ResponseWriter,
	redis *models.RedisLogger /*, user *models.Account*/, form fileUploadForm) {
	user := &models.Account{}

	if len(form.Token) > 0 {
		id := redis.OnlineUser(form.Token)
		if find, _ := user.FindByUserid(id); !find {
			writeResponse(request.RequestURI, resp, nil, errors.NewError(errors.NotFoundError))
			return
		}
	}

	var file models.File

	filedata, header, err := request.FormFile("filedata")
	if err != nil {
		log.Println(err)
		writeResponse(request.RequestURI, resp, nil, errors.NewError(errors.FileNotFoundError))
		return
	}

	if form.Width > 0 || form.Height > 0 {
		img, _, err := image.Decode(filedata)
		if err != nil {
			log.Println(err)
			writeResponse(request.RequestURI, resp, nil, errors.NewError(errors.InvalidFileError))
			return
		}

		fid, err := Weedfs.Master().AssignN(2)
		if err != nil {
			log.Println(err)
			writeResponse(request.RequestURI, resp, nil, errors.NewError(errors.FileUploadError))
			return
		}
		file.Fid = fid

		thumbnail := resize.Thumbnail(uint(form.Width), uint(form.Height), img, resize.MitchellNetravali)
		vol, err := Weedfs.Volume(fid, "")
		if err != nil {
			log.Println(err)
			writeResponse(request.RequestURI, resp, nil, errors.NewError(errors.FileUploadError))
			return
		}

		buf := &bytes.Buffer{}
		if err := jpeg.Encode(buf, thumbnail, nil); err != nil {
			log.Println(err)
			writeResponse(request.RequestURI, resp, nil, errors.NewError(errors.FileUploadError))
			return
		}

		length, err := vol.Upload(fid, 0, header.Filename, "image/jpeg", buf)
		if err != nil {
			log.Println(err)
			writeResponse(request.RequestURI, resp, nil, errors.NewError(errors.FileUploadError))
			return
		}
		file.Length = length

		filedata.Seek(0, 0)
		if _, err := vol.Upload(fid, 1, header.Filename, header.Header.Get("Content-Type"), filedata); err != nil {
			log.Println(err)
			writeResponse(request.RequestURI, resp, nil, errors.NewError(errors.FileUploadError))
			return
		}
	} else {
		fid, length, err := Weedfs.Master().Submit(header.Filename, header.Header.Get("Content-Type"), filedata)
		if err != nil {
			writeResponse(request.RequestURI, resp, nil, errors.NewError(errors.FileUploadError))
			return
		}
		//log.Println(fid, length, header.Filename, header.Header.Get("Content-Type"))

		file.Fid = fid
		file.Length = length
	}

	filedata.Seek(0, 0)

	file.Name = header.Filename
	file.ContentType = header.Header.Get("Content-Type")
	file.Md5 = FileMd5(filedata)
	file.Owner = user.Id
	file.UploadDate = time.Now()
	if err := file.Save(); err != nil {
		writeResponse(request.RequestURI, resp, nil, err)
		return
	}

	url, _, _ := Weedfs.GetUrl(file.Fid)
	respData := map[string]interface{}{"fileid": file.Fid, "fileurl": url}

	writeResponse(request.RequestURI, resp, respData, nil)
}
Esempio n. 2
0
func ueditorHandler(r *http.Request, w http.ResponseWriter, form ueditorForm) {
	var result interface{}

	switch form.Action {
	case "config":
		result = config

	case "uploadimage":
		if r.Method == "OPTIONS" {
			writeResponse(w, nil)
			return
		}
		filedata, header, err := r.FormFile(config["fileFieldName"].(string))
		if err != nil {
			log.Println(err)
			result = map[string]interface{}{
				"state": "找不到上传文件",
			}
			break
		}

		fid, length, err := controllers.Weedfs.Master().Submit(header.Filename,
			header.Header.Get("Content-Type"), filedata)
		if err != nil {
			log.Println(err)
			result = map[string]interface{}{
				"state": "文件上传时出错",
			}
			break
		}

		var file models.File
		file.Fid = fid
		file.Name = header.Filename
		file.ContentType = header.Header.Get("Content-Type")
		file.Length = length
		file.Owner = "admin"
		file.UploadDate = time.Now()
		if err := file.Save(); err != nil {
			result = map[string]interface{}{
				"state": "文件保存时出错",
			}
			break
		}

		url, _, _ := controllers.Weedfs.GetUrl(fid)
		result = map[string]interface{}{
			"state":    "SUCCESS",
			"url":      url,
			"title":    file.Name,
			"original": file.Name,
			"type":     strings.Split(file.Name, ".")[1],
			"size":     file.Length,
		}
	default:
		result = map[string]interface{}{
			"state": "请求地址出错",
		}
	}

	if len(form.Callback) > 0 {
		if strings.Contains(form.Callback, "_") {
			data, _ := json.Marshal(result)
			writeRawResponse(w, "application/javascript", []byte(form.Callback+"("+string(data)+")"))
			return
		} else {
			result = map[string]interface{}{
				"state": "callback参数不合法",
			}
		}
	}

	data, _ := json.Marshal(result)
	writeRawResponse(w, "text/html; charset=utf-8", data)

}