Ejemplo n.º 1
0
func (r *Remote) Upsert(parentId string, file *types.File, body io.Reader) (f *types.File, err error) {
	uploaded := &drive.File{
		Title:   file.Name,
		Parents: []*drive.ParentReference{&drive.ParentReference{Id: parentId}},
	}
	if file.IsDir {
		uploaded.MimeType = "application/vnd.google-apps.folder"
	}

	if file.Id == "" {
		req := r.service.Files.Insert(uploaded)
		if !file.IsDir && body != nil {
			req = req.Media(body)
		}
		if uploaded, err = req.Do(); err != nil {
			return
		}
		return types.NewRemoteFile(uploaded), nil
	}
	// update the existing
	req := r.service.Files.Update(file.Id, uploaded)
	if !file.IsDir && body != nil {
		req = req.Media(body)
	}
	if uploaded, err = req.Do(); err != nil {
		return
	}
	return types.NewRemoteFile(uploaded), nil
}
Ejemplo n.º 2
0
func (r *Remote) FindById(id string) (file *types.File, err error) {
	req := r.service.Files.Get(id)
	var f *drive.File
	if f, err = req.Do(); err != nil {
		return
	}
	return types.NewRemoteFile(f), nil
}
Ejemplo n.º 3
0
func (r *Remote) FindByParentId(parentId string) (files []*types.File, err error) {
	req := r.service.Files.List()
	// TODO: use field selectors
	req.Q(fmt.Sprintf("'%s' in parents and trashed=false", parentId))
	results, err := req.Do()
	if err != nil {
		return
	}
	for _, f := range results.Items {
		if !strings.HasPrefix(f.Title, ".") { // ignore hidden files
			files = append(files, types.NewRemoteFile(f))
		}
	}
	return
}
Ejemplo n.º 4
0
func (r *Remote) findByPathRecv(parentId string, p []string) (file *types.File, err error) {
	// find the file or directory under parentId and titled with p[0]
	req := r.service.Files.List()
	// TODO: use field selectors
	req.Q(fmt.Sprintf("'%s' in parents and title = '%s' and trashed=false", parentId, p[0]))
	files, err := req.Do()
	if err != nil || len(files.Items) < 1 {
		// TODO: make sure only 404s are handled here
		return nil, ErrPathNotExists
	}
	file = types.NewRemoteFile(files.Items[0])
	if len(p) == 1 {
		return file, nil
	}
	return r.findByPathRecv(file.Id, p[1:])
}