Exemple #1
0
// 新建媒体文件
func newMedia(params interface{}) string {
	username := params.([]interface{})[1].(string)
	password := params.([]interface{})[2].(string)
	result := login(username, password)

	if result {
		name := params.([]interface{})[3].(map[string]interface{})["name"]
		filetype := params.([]interface{})[3].(map[string]interface{})["type"]
		bits := params.([]interface{})[3].(map[string]interface{})["bits"]

		err := utils.ParseMedia("static/upload/"+name.(string), bits.(string))

		if nil != err {
			str := com.ReadFile("views/rpcxml/response_failed.xml")
			return fmt.Sprintf(str, "上传写入失败")
		}

		// 文件保存到OSS
		t := time.Now()
		ossFilename := fmt.Sprintf("%d/%d/%d/%s", t.Year(), t.Month(), t.Day(), name.(string))
		err = utils.OssStore(ossFilename, "static/upload/"+name.(string))

		if nil != err {
			str := com.ReadFile("views/rpcxml/response_failed.xml")
			return fmt.Sprintf(str, "图片保存到OSS失败")
		} else {
			os.Remove("./static/upload/" + name.(string))
			id, err := AddFile(name.(string), ossFilename, "oss", filetype.(string))
			if nil != err {
				log.Println(err)
				str := com.ReadFile("views/rpcxml/response_failed.xml")
				return fmt.Sprintf(str, "图片信息添加到数据库失败")
			}
			str := com.ReadFile("views/rpcxml/response_new_media_object.xml")
			return fmt.Sprintf(str, id, name.(string), utils.OssGetURL(ossFilename), filetype.(string))
		}
	} else {
		return com.ReadFile("views/rpcxml/response_login_failed.xml")
	}

}
Exemple #2
0
func (this *UploadController) Post() {
	// if not login, permission deny
	user := this.GetSession("username")
	if user == nil {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "login first please", "refer": nil}
		this.ServeJson()
		return
	}

	// 获取上传文件
	f, h, err := this.GetFile("upfile")
	if nil == err {
		f.Close()
	}

	if nil != err {
		this.Data["json"] = map[string]interface{}{"result": false, "msg": "upload failed", "refer": nil}
		this.ServeJson()
		return
	}

	mime := h.Header.Get("Content-Type")

	// 文件保存到本地
	err = this.SaveToFile("upfile", "./static/upload/"+h.Filename)
	if nil != err {
		this.Data["json"] = map[string]interface{}{
			"result": false,
			"state":  "upload to local FAILED",
			"msg":    "upload failed",
			"refer":  nil,
		}
		this.ServeJson()
		return
	}

	// 文件保存到OSS
	t := time.Now()
	ossFilename := fmt.Sprintf("%d/%d/%d/%s", t.Year(), t.Month(), t.Day(), h.Filename)
	err = utils.OssStore(ossFilename, "static/upload/"+h.Filename)

	// 文件名过长
	if len(h.Filename) > 96 {
		this.Data["json"] = map[string]interface{}{
			"result": false,
			"state":  "upload to oss FAILED, " + fmt.Sprint(err),
			"msg":    "upload failed",
			"refer":  nil,
		}
		this.ServeJson()
	}

	if nil != err {
		// 保存到oss失败
		this.Data["json"] = map[string]interface{}{
			"result": false,
			"state":  "upload to oss FAILED, " + fmt.Sprint(err),
			"msg":    "upload failed",
			"refer":  nil,
		}
		this.ServeJson()
		return
	} else {
		// 保存到oss成功
		os.Remove("./static/upload/" + h.Filename)
		if _, err = AddFile(h.Filename, ossFilename, "oss", mime); nil != err {
			this.Data["json"] = map[string]interface{}{
				"result": false,
				"state":  "save info to database FAILED, " + fmt.Sprint(err),
				"msg":    "upload failed",
				"refer":  nil,
			}
			this.ServeJson()
		}
	}

	this.Data["json"] = map[string]interface{}{
		"url":      utils.OssGetURL(ossFilename), //保存后的文件路径
		"title":    "",                           //文件描述,对图片来说在前端会添加到title属性上
		"original": h.Filename,                   //原始文件名
		"state":    "SUCCESS",
	}
	this.ServeJson()
}