Beispiel #1
0
//列出图片
func listFiles(data *ListReqData, list *KindList) error {
	t, err := time.Parse("2006年1月", list.CurrentDirPath)
	if err != nil {
		glog.Errorln("解析时间错误:", err)
		return err
	}

	//取得bucket
	bucket, err := bucketdb.FindByName(data.Bucket)
	if err != nil {
		return err
	}

	list.CurrentUrl = bucket.ImgBaseUrl()
	list.MoveupDirPath = t.Format("2006年")
	prefix := t.Format(IMG_PRE_FMT)

	//取得图片列表
	list.FileList = make([]*KindFile, 0, 10)
	client := rsf.New(&digest.Mac{bucket.Ak, []byte(bucket.Sk)})
	marker := ""
	limit := 1000

	//    "is_dir": false,
	//    "has_file": false,
	//    "filesize": 208736,
	//    "is_photo": true,
	//    "filetype": "jpg",
	//    "filename": "1241601537255682809.jpg",
	//    "datetime": "2011-08-02 15:32:43"
	var es []rsf.ListItem
	for err == nil {
		es, marker, err = client.ListPrefix(nil, bucket.Name, prefix, marker, limit)
		for _, item := range es {
			f := &KindFile{
				IsDir:    false,
				HasFile:  false,
				IsPhoto:  true,
				Filesize: item.Fsize,
				Filetype: item.MimeType,
				Filename: item.Key,
				Datetime: time.Unix(item.PutTime, 0).Format("2006-01-02 15:04:05"),
			}
			list.FileList = append(list.FileList, f)
		}
	}
	if err != io.EOF {
		//非预期的错误
		glog.Errorln("listAll failed:", err)
		return err
	}
	list.TotalCount = len(list.FileList)
	list.Order = data.Order
	sort.Sort(list)
	return nil
}
Beispiel #2
0
func One() martini.Handler {
	return func(r render.Render, params martini.Params) {
		bucket, err := db.FindByName(params["name"])
		if err != nil {
			comm.JsonErr(r, err)
		} else {
			comm.JsonContent(r, bucket)
		}
	}
}
Beispiel #3
0
//martini Handler
func UploadHandlers() []martini.Handler {
	var upload = func(fileData FileData, data UploadData, w http.ResponseWriter) {
		retJsonp := &UploadRetJsonp{MessengerJs: MessengerJs, UpTime: data.UpTime}

		if strings.ToUpper(data.Dir) != "IMAGE" {
			retJsonp.Err(w, "dir wrong:"+data.Dir)
			return
		}

		imgFile, err := fileData.ImgFile.Open()
		if err != nil {
			retJsonp.Err(w, "ImgFile:"+err.Error())
			return
		}

		//取得bucket
		bucket, err := bucketdb.FindByName(data.Bucket)
		if err != nil {
			retJsonp.Err(w, "FindByName:"+err.Error())
			return
		}

		if bucket.Uptoken == "" {
			if err := bucket.NewUptoken(); err != nil {
				retJsonp.Err(w, "NewUptoken:"+err.Error())
				return
			}
			if err := bucket.Save(); err != nil {
				retJsonp.Err(w, "Save:"+err.Error())
				return
			}
		}

		//上传内容到Qiniu
		var ret qio.PutRet
		// ret       	变量用于存取返回的信息,详情见 qio.PutRet
		// uptoken   	为业务服务器端生成的上传口令
		// key:imgName	为文件存储的标识
		// r:imgFile   	为io.Reader类型,用于从其读取数据
		// extra     	为上传文件的额外信息,可为空, 详情见 qio.PutExtra, 可选
		err = qio.Put(nil, &ret, bucket.Uptoken, fileData.ImgName(), imgFile, nil)
		if err != nil {
			bucket.LogErr()
			retJsonp.Err(w, "Put:"+err.Error())
			return
		}

		//上传成功,返回给KindEditor
		//w.Header().Set("Content-type", "application/json")
		retJsonp.Respose(w, bucket.ImgUrl(ret.Key))
	}
	return []martini.Handler{binding.MultipartForm(FileData{}), binding.Form(UploadData{}), upload}
}
Beispiel #4
0
func DeleteHandlers() []martini.Handler {
	var bind = binding.Bind(DeleteReqData{})
	var deleteHandler = func(data DeleteReqData, w http.ResponseWriter) {
		index := strings.LastIndex(data.Url, "/")
		runes := []rune(data.Url)
		key := string(runes[index+1:])
		bucket, err := bucketdb.FindByName(data.Bucket)
		rsCli := rs.New(&digest.Mac{bucket.Ak, []byte(bucket.Sk)})
		err = rsCli.Delete(nil, bucket.Name, key)
		if err != nil {
			//产生错误
			glog.Infoln("删除图片错误:", err)
			io.WriteString(w, data.Callback+"({error:1})")
			return
		}
		io.WriteString(w, data.Callback+"({error:0})")
	}
	return []martini.Handler{bind, deleteHandler}
}