From 45c17fe8b82f249682ea739de2cc089631849fd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=AA=20Th=C3=A0nh=20=C4=90=E1=BA=A1t?= <91755733+lds217@users.noreply.github.com> Date: Tue, 22 Jul 2025 23:07:29 +0700 Subject: [PATCH] [update] Implement file override logic in GoogleDriveServiceOAuth. Add functionality to delete existing files with the same name before uploading new files to Google Drive. Introduce a helper method to find files by name within a specified folder, enhancing file management capabilities. --- .../service/GoogleDriveServiceOAuth.java | 35 ++++++++++++++++++ src/main/resources/database/BSK.db | Bin 1196032 -> 1196032 bytes 2 files changed, 35 insertions(+) diff --git a/src/main/java/BsK/server/service/GoogleDriveServiceOAuth.java b/src/main/java/BsK/server/service/GoogleDriveServiceOAuth.java index f69babf..b8c8673 100644 --- a/src/main/java/BsK/server/service/GoogleDriveServiceOAuth.java +++ b/src/main/java/BsK/server/service/GoogleDriveServiceOAuth.java @@ -415,6 +415,20 @@ public class GoogleDriveServiceOAuth { */ public String uploadFileToFolder(String folderId, java.io.File localFile, String fileName) throws IOException { String mimeType = determineMimeType(fileName); + + // --- OVERRIDE LOGIC START --- + // 1. Find any existing files with the same name in the target folder + findFilesByName(folderId, fileName).forEach(file -> { + try { + log.info("Deleting existing file '{}' (ID: {}) to override.", file.getName(), file.getId()); + driveService.files().delete(file.getId()).execute(); + } catch (IOException e) { + log.error("Failed to delete existing file (ID: {})", file.getId(), e); + // We wrap in a RuntimeException because the lambda can't throw a checked IOException + throw new RuntimeException("Failed to delete existing file for override", e); + } + }); + // --- OVERRIDE LOGIC END --- File fileMetadata = new File(); fileMetadata.setName(fileName); @@ -429,4 +443,25 @@ public class GoogleDriveServiceOAuth { log.info("Uploaded file: {} to folder {} (File ID: {})", fileName, folderId, uploadedFile.getId()); return uploadedFile.getId(); } + + /** + * Helper method to find all files with a specific name inside a parent folder. + * @param parentFolderId The ID of the folder to search within. + * @param fileName The exact name of the files to find. + * @return A list of File objects matching the name. + * @throws IOException if the Google Drive API call fails. + */ + private List findFilesByName(String parentFolderId, String fileName) throws IOException { + String query = String.format("name = '%s' and '%s' in parents and trashed = false", + fileName.replace("'", "\\'"), // Escape single quotes in filename + parentFolderId); + + FileList result = driveService.files().list() + .setQ(query) + .setSpaces("drive") + .setFields("nextPageToken, files(id, name)") + .execute(); + + return result.getFiles(); + } } \ No newline at end of file diff --git a/src/main/resources/database/BSK.db b/src/main/resources/database/BSK.db index 75dbbc25426f5f8744d3192a0fb7bba90b0dff92..dd959d494dbf682277020564c2e0d876255ff72e 100644 GIT binary patch delta 121 zcmV-<0EYj7fJuOWNst=>K#?3n0YHIZg3;1BGA&g~4~1Y5wO|pw-vbu{?2{2NXt#*q5snNG1cH7X76kwS2>=xp b0E0P~FYOWBmvHzI3YSjt5gWHA?h)EiOH(K| delta 132 zcmZo@@N8)CoFL6;H&Mo!(XKI}HG#1;fvGitxix{MHG#D?fvq)xy)}WOHG#7=fvYuv zyETEQHG#J^fv+`zzcoQ%Yl7h3`^<_=Zzc;GWNhzvAlSzv#mCCP$jEHYz|hLuR3ak= k