From 9d09ee133d66bca3c2a70dbbcc6742bd34b6d3bb Mon Sep 17 00:00:00 2001 From: jenfonro <799170122@qq.com> Date: Tue, 4 Nov 2025 09:26:20 +0800 Subject: [PATCH] fix(google_driver): fix google link file display size (#1335) * fix file link display size * fix performance and field * cn to en notes --------- Co-authored-by: ShenLin <773933146@qq.com> --- drivers/google_drive/util.go | 81 +++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/drivers/google_drive/util.go b/drivers/google_drive/util.go index 372b6254..e2e609a8 100644 --- a/drivers/google_drive/util.go +++ b/drivers/google_drive/util.go @@ -27,6 +27,14 @@ import ( // do others that not defined in Driver interface +// Google Drive API field constants +const ( + // File list query fields + FilesListFields = "files(id,name,mimeType,size,modifiedTime,createdTime,thumbnailLink,shortcutDetails,md5Checksum,sha1Checksum,sha256Checksum),nextPageToken" + // Single file query fields + FileInfoFields = "id,name,mimeType,size,md5Checksum,sha1Checksum,sha256Checksum" +) + type googleDriveServiceAccount struct { // Type string `json:"type"` // ProjectID string `json:"project_id"` @@ -235,7 +243,7 @@ func (d *GoogleDrive) getFiles(id string) ([]File, error) { } query := map[string]string{ "orderBy": orderBy, - "fields": "files(id,name,mimeType,size,modifiedTime,createdTime,thumbnailLink,shortcutDetails,md5Checksum,sha1Checksum,sha256Checksum),nextPageToken", + "fields": FilesListFields, "pageSize": "1000", "q": fmt.Sprintf("'%s' in parents and trashed = false", id), //"includeItemsFromAllDrives": "true", @@ -249,11 +257,82 @@ func (d *GoogleDrive) getFiles(id string) ([]File, error) { return nil, err } pageToken = resp.NextPageToken + + // Batch process shortcuts, API calls only for file shortcuts + shortcutTargetIds := make([]string, 0) + shortcutIndices := make([]int, 0) + + // Collect target IDs of all file shortcuts (skip folder shortcuts) + for i := range resp.Files { + if resp.Files[i].MimeType == "application/vnd.google-apps.shortcut" && + resp.Files[i].ShortcutDetails.TargetId != "" && + resp.Files[i].ShortcutDetails.TargetMimeType != "application/vnd.google-apps.folder" { + shortcutTargetIds = append(shortcutTargetIds, resp.Files[i].ShortcutDetails.TargetId) + shortcutIndices = append(shortcutIndices, i) + } + } + + // Batch get target file info (only for file shortcuts) + if len(shortcutTargetIds) > 0 { + targetFiles := d.batchGetTargetFilesInfo(shortcutTargetIds) + // Update shortcut file info + for j, targetId := range shortcutTargetIds { + if targetFile, exists := targetFiles[targetId]; exists { + fileIndex := shortcutIndices[j] + if targetFile.Size != "" { + resp.Files[fileIndex].Size = targetFile.Size + } + if targetFile.MD5Checksum != "" { + resp.Files[fileIndex].MD5Checksum = targetFile.MD5Checksum + } + if targetFile.SHA1Checksum != "" { + resp.Files[fileIndex].SHA1Checksum = targetFile.SHA1Checksum + } + if targetFile.SHA256Checksum != "" { + resp.Files[fileIndex].SHA256Checksum = targetFile.SHA256Checksum + } + } + } + } + res = append(res, resp.Files...) } return res, nil } +// getTargetFileInfo gets target file details for shortcuts +func (d *GoogleDrive) getTargetFileInfo(targetId string) (File, error) { + var targetFile File + url := fmt.Sprintf("https://www.googleapis.com/drive/v3/files/%s", targetId) + query := map[string]string{ + "fields": FileInfoFields, + } + _, err := d.request(url, http.MethodGet, func(req *resty.Request) { + req.SetQueryParams(query) + }, &targetFile) + if err != nil { + return File{}, err + } + return targetFile, nil +} + +// batchGetTargetFilesInfo batch gets target file info, sequential processing to avoid concurrency complexity +func (d *GoogleDrive) batchGetTargetFilesInfo(targetIds []string) map[string]File { + if len(targetIds) == 0 { + return make(map[string]File) + } + + result := make(map[string]File) + // Sequential processing to avoid concurrency complexity + for _, targetId := range targetIds { + file, err := d.getTargetFileInfo(targetId) + if err == nil { + result[targetId] = file + } + } + return result +} + func (d *GoogleDrive) chunkUpload(ctx context.Context, file model.FileStreamer, url string, up driver.UpdateProgress) error { defaultChunkSize := d.ChunkSize * 1024 * 1024 ss, err := stream.NewStreamSectionReader(file, int(defaultChunkSize), &up)