Exemplo n.º 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
}
Exemplo n.º 2
0
func (this *ListBucket) List(bucket string, prefix string, listResultFile string) (retErr error) {
	fp, openErr := os.OpenFile(listResultFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
	if openErr != nil {
		retErr = openErr
		log.Error(fmt.Sprintf("Failed to open list result file `%s'", listResultFile))
		return
	}
	defer fp.Close()
	bw := bufio.NewWriter(fp)

	mac := digest.Mac{this.AccessKey, []byte(this.SecretKey)}
	client := rsf.New(&mac)
	marker := ""
	limit := 1000
	run := true
	maxRetryTimes := 5
	retryTimes := 1
	for run {
		entries, markerOut, err := client.ListPrefix(nil, bucket, prefix, marker, limit)
		if err != nil {
			if err == io.EOF {
				run = false
			} else {
				log.Error(fmt.Sprintf("List error for marker `%s'", marker), err)
				if retryTimes <= maxRetryTimes {
					log.Debug(fmt.Sprintf("Retry list for marker `%s' for `%d' time", marker, retryTimes))
					retryTimes += 1
					continue
				} else {
					log.Error(fmt.Sprintf("List failed too many times for `%s'", marker))
					break
				}
			}
		} else {
			retryTimes = 1
			if markerOut == "" {
				run = false
			} else {
				marker = markerOut
			}
		}
		//append entries
		for _, entry := range entries {
			lineData := fmt.Sprintf("%s\t%d\t%s\t%d\t%s\t%s\r\n", entry.Key, entry.Fsize, entry.Hash, entry.PutTime, entry.MimeType, entry.EndUser)
			_, wErr := bw.WriteString(lineData)
			if wErr != nil {
				log.Error(fmt.Sprintf("Write line data `%s' to list result file failed.", lineData))
			}
		}
		fErr := bw.Flush()
		if fErr != nil {
			log.Error("Flush data to list result file error", err)
		}
	}
	return
}
Exemplo n.º 3
0
func (factory *QiniuDriverFactory) NewDriver() (server.Driver, error) {
	mac := &digest.Mac{conf.ACCESS_KEY, []byte(conf.SECRET_KEY)}
	client := rs.New(mac)
	client2 := rsf.New(mac)
	return &QiniuDriver{"/", client, client2, factory.bucket}, nil
}