diff --git a/src/main/java/com/rudakova/media_archive/google/GoogleService.java b/src/main/java/com/rudakova/media_archive/google/GoogleService.java index bb913f35cb42d087fa6d7ca8d4b5d80e2a6a8806..a116e56f09c28969b7e1298f20767af2a43af22e 100644 --- a/src/main/java/com/rudakova/media_archive/google/GoogleService.java +++ b/src/main/java/com/rudakova/media_archive/google/GoogleService.java @@ -19,7 +19,6 @@ import com.rudakova.media_archive.service.AuthService; import com.rudakova.media_archive.service.RestService; import com.rudakova.media_archive.util.FileUtil; import com.rudakova.media_archive.util.HashCalculator; -import org.mortbay.util.MultiMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -29,7 +28,6 @@ import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; -import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.io.BufferedInputStream; @@ -186,28 +184,35 @@ public class GoogleService { } } - public void checkUpdate(String folderId) throws IOException { + public int checkUpdate(String folderId, Instant date) throws IOException { Drive service = getDrive(); - Account account = authService.getLoggedInAccount(); + int count = 0; + + String modifiedTime = date.toString(); + modifiedTime = modifiedTime.substring(0, modifiedTime.lastIndexOf("Z") + 1); + modifiedTime = " and modifiedTime > '" + modifiedTime + "'"; - String nextPageToken = null, folderQuery; + String nextPageToken = null, query; Queue<String> folders = new ArrayDeque<>(); folders.offer(folderId); while (!folders.isEmpty()) { folderId = folders.poll(); - folderQuery = "'" + folderId + "' in parents"; + query = "'" + folderId + "' in parents" + modifiedTime; do { FileList result = service.files().list() - .setQ(folderQuery) + .setQ(query) .setPageToken(nextPageToken) .setFields("nextPageToken, files(" + FILE_FIELDS + ")") .execute(); nextPageToken = saveFileToBd(folderId, account, folders, result); + count += result.getFiles().size(); } while (nextPageToken != null); } + + return count; } public Drive getDrive() throws IOException { diff --git a/src/main/java/com/rudakova/media_archive/model/Account.java b/src/main/java/com/rudakova/media_archive/model/Account.java index 035a4613d4f1371c070d3f9074c0fa57be1b4510..528e4c2b27e7d59704d174e3a1db6f90d6db3d33 100644 --- a/src/main/java/com/rudakova/media_archive/model/Account.java +++ b/src/main/java/com/rudakova/media_archive/model/Account.java @@ -3,8 +3,6 @@ package com.rudakova.media_archive.model; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; -import java.time.Instant; - @Document public class Account { diff --git a/src/main/java/com/rudakova/media_archive/web/NotificationController.java b/src/main/java/com/rudakova/media_archive/web/NotificationController.java index 6ae7ad12c28c9ef880a4da7df3d625b60d454867..28eca86cc9e77805a1858e97f4a8717be842386b 100644 --- a/src/main/java/com/rudakova/media_archive/web/NotificationController.java +++ b/src/main/java/com/rudakova/media_archive/web/NotificationController.java @@ -13,6 +13,8 @@ import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.io.IOException; +import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.Collections; import java.util.List; import java.util.Map; @@ -35,20 +37,23 @@ public class NotificationController { .stream() .collect(Collectors.toMap(h -> h, request::getHeader)); LOG.info(String.valueOf(headers)); - String state = headers.get("X-Goog-Resource-State"); - String uri = request.getHeader("X-Goog-Resource-URI"); + String state = headers.get("x-goog-resource-state"); + String uri = request.getHeader("x-goog-resource-uri"); String id = uri.substring(uri.lastIndexOf("/")); + LOG.info("Values: {}, {}", state, id); if (state != null && state.equals("trash")) { List<String> ids = googleService.getFiles(id); LOG.info("Deleted from google: {}", ids); for (String trashId : ids) { fileRepository.deleteById(trashId); } - } else if (state != null && state.equals("update")) { + } else if (state != null && (state.equals("update") || state.equals("untrash"))) { String changed = headers.get("x-goog-changed"); - if (changed.equals("children")) { - googleService.checkUpdate(id); + int count = 0; + if (changed == null || changed.equals("children")) { + count = googleService.checkUpdate(id, Instant.now().minus(10, ChronoUnit.MINUTES)); } + LOG.info("Changes: {}", count); } return new ResponseEntity<>(HttpStatus.OK); } diff --git a/target/MediaArchive-1.0-SNAPSHOT.jar b/target/MediaArchive-1.0-SNAPSHOT.jar index 84f66aa5a3b164a9a1ee46cdb57007728dc908ca..4ccf1a96d73a6d804b3072af724064597f3f1e2c 100644 Binary files a/target/MediaArchive-1.0-SNAPSHOT.jar and b/target/MediaArchive-1.0-SNAPSHOT.jar differ