func (fs *GDriveFileSystem) Get(p string) (webdav.StatusCode, io.ReadCloser, int64) { pFile := fs.getFile(p, false) if pFile == nil { return webdav.StatusCode(404), nil, -1 } f := pFile.file downloadUrl := f.DownloadUrl log.Debug("downloadUrl=", downloadUrl) if downloadUrl == "" { log.Error("No download url: ", f) return webdav.StatusCode(500), nil, -1 } req, err := http.NewRequest("GET", downloadUrl, nil) if err != nil { log.Error("NewRequest ", err) return webdav.StatusCode(500), nil, -1 } resp, err := fs.transport.RoundTrip(req) if err != nil { log.Error("RoundTrip ", err) return webdav.StatusCode(500), nil, -1 } return webdav.StatusCode(200), resp.Body, f.FileSize }
func (fs *GDriveFileSystem) Put(p string, bytes io.ReadCloser) webdav.StatusCode { defer bytes.Close() parent := path.Dir(p) base := path.Base(p) parentId := fs.getFileId(parent, true) if parentId == "" { log.Errorf("ERROR: Parent not found") return webdav.StatusCode(http.StatusConflict) // 409 } parentRef := &drive.ParentReference{ Id: parentId, IsRoot: "parent" == "/", } f := &drive.File{ Title: base, Parents: []*drive.ParentReference{parentRef}, } _, err := fs.client.Files.Insert(f).Media(bytes).Do() if err != nil { log.Errorf("can't put: %v", err) return webdav.StatusCode(500) } fs.invalidatePath(p) fs.invalidatePath(parent) return webdav.StatusCode(201) }
func (fs *GDriveFileSystem) PropList(p string, depth int, props []string) (webdav.StatusCode, map[string][]webdav.PropertyValue) { f := fs.getFile(p, false) log.Debug("PropList f=", f, " depth=", depth) if f == nil { log.Debug("Can't find file ", p) return webdav.StatusCode(404), nil } if depth != 0 && depth != 1 { log.Error("Unsupported depth ", depth) return webdav.StatusCode(500), nil } files := []*fileAndPath{f} if depth == 1 { query := fmt.Sprintf("'%s' in parents", f.file.Id) r, err := fs.client.Files.List().Q(query).Do() if err != nil { log.Error("Can't list children ", err) return webdav.StatusCode(505), nil } for _, file := range r.Items { if ignoreFile(file) { continue } files = append(files, &fileAndPath{file: file, path: path.Join(p, file.Title)}) } } return fs.listPropsFromFiles(files, props) }
func (fs *GDriveFileSystem) listPropsFromFiles(files []*fileAndPath, props []string) (webdav.StatusCode, map[string][]webdav.PropertyValue) { result := make(map[string][]webdav.PropertyValue) for _, fp := range files { f := fp.file var pValues []webdav.PropertyValue for _, p := range props { switch p { case "getcontentlength": pValues = append(pValues, webdav.GetContentLengthPropertyValue(f.FileSize)) case "displayname": pValues = append(pValues, webdav.DisplayNamePropertyValue(f.Title)) case "resourcetype": b := false if isFolder(f) { b = true } pValues = append(pValues, webdav.ResourceTypePropertyValue(b)) case "getcontenttype": s := f.MimeType if isFolder(f) { s = "httpd/unix-directory" } pValues = append(pValues, webdav.GetContentTypePropertyValue(s)) case "getlastmodified": t, err := time.Parse(time.RFC3339, f.ModifiedDate) if err != nil { log.Error("Can't parse modified date ", err, " ", f.ModifiedDate) return webdav.StatusCode(500), nil } pValues = append(pValues, webdav.GetLastModifiedPropertyValue(t.Unix())) case "creationdate": t, err := time.Parse(time.RFC3339, f.CreatedDate) if err != nil { log.Error("Can't parse CreationDate date ", err, " ", f.CreatedDate) return webdav.StatusCode(500), nil } pValues = append(pValues, webdav.GetLastModifiedPropertyValue(t.Unix())) case "getetag": pValues = append(pValues, webdav.GetEtagPropertyValue("")) case "quota-available-bytes": about, err := fs.about() if err != nil { log.Error("Can't get about info: ", err) return webdav.StatusCode(500), nil } pValues = append(pValues, webdav.QuotaAvailableBytesPropertyValue(about.QuotaBytesTotal-about.QuotaBytesUsed)) case "quota-used-bytes": about, err := fs.about() if err != nil { log.Error("Can't get about info: ", err) return webdav.StatusCode(500), nil } pValues = append(pValues, webdav.QuotaUsedBytesPropertyValue(about.QuotaBytesUsed)) case "quotaused", "quota": // ignore continue default: log.Error("Unsupported property: ", p) return webdav.StatusCode(500), nil } } result[fp.path] = pValues } return webdav.StatusCode(200), result }