| 651 | } |
| 652 | |
| 653 | GoogleDriveProvider::LookupStatus GoogleDriveProvider::FindFileInFolderStatus( |
| 654 | const std::string& name, const std::string& folderId, std::string* outId) { |
| 655 | std::string q = "name='" + EscapeQuery(name) + "'" |
| 656 | " and '" + EscapeQuery(folderId) + "' in parents" |
| 657 | " and mimeType!='application/vnd.google-apps.folder'" |
| 658 | " and trashed=false"; |
| 659 | auto r = ApiGet("/drive/v3/files?q=" + UrlEncode(q) + |
| 660 | "&fields=files(id,createdTime,size,modifiedTime)&orderBy=modifiedTime desc&pageSize=10"); |
| 661 | if (r.status == 404) { |
| 662 | InvalidateFolderById(folderId); |
| 663 | return LookupStatus::Missing; |
| 664 | } |
| 665 | if (r.status != 200) return LookupStatus::Error; |
| 666 | auto j = Json::Parse(r.body); |
| 667 | auto& files = j["files"]; |
| 668 | if (files.size() == 0) { |
| 669 | InvalidateFileChild(folderId, name); |
| 670 | return LookupStatus::Missing; |
| 671 | } |
| 672 | // Keep the most recently modified file (newest data wins in cross-PC races) |
| 673 | std::string keepId = files[(size_t)0]["id"].str(); |
| 674 | if (files.size() > 1) { |
| 675 | LOG("[GDrive] Duplicate file '%s' in folder %s (%zu copies); keeping newest id=%s, deleting rest", |
| 676 | name.c_str(), folderId.c_str(), files.size(), keepId.c_str()); |
| 677 | for (size_t i = 1; i < files.size(); ++i) { |
| 678 | std::string dupId = files[i]["id"].str(); |
| 679 | if (!DeleteById(dupId)) { |
| 680 | LOG("[GDrive] WARNING: failed to delete duplicate file %s", dupId.c_str()); |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | CacheFileChild(folderId, name, keepId); |
| 685 | if (outId) *outId = keepId; |
| 686 | return LookupStatus::Exists; |
| 687 | } |
| 688 | |
| 689 | GoogleDriveProvider::DuplicateFileIdsResult GoogleDriveProvider::FindDuplicateFileIdsInFolder( |
| 690 | const std::string& name, const std::string& folderId) { |