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 7c53c712feab765095abf3fcf7a6511232442bc4..85c921893d150e53872bd56698ae7348bc68ede8 100644 --- a/src/main/java/com/rudakova/media_archive/google/GoogleService.java +++ b/src/main/java/com/rudakova/media_archive/google/GoogleService.java @@ -11,6 +11,7 @@ import com.google.api.services.drive.model.FileList; import com.rudakova.media_archive.converter.FileConverter; import com.rudakova.media_archive.dto.NotificationDto; import com.rudakova.media_archive.model.Account; +import com.rudakova.media_archive.model.Channel; import com.rudakova.media_archive.model.File; import com.rudakova.media_archive.model.FolderList; import com.rudakova.media_archive.repository.AccountRepository; @@ -270,34 +271,61 @@ public class GoogleService { Queue<String> folders, com.google.api.services.drive.model.File file) { String mimeType = file.getMimeType(); NotificationDto.Address address; + File savedFile; if (mimeType.equals("application/vnd.google-apps.folder")) { folders.offer(file.getId()); address = NotificationDto.Address.FOLDER; - fileRepository.save(FileConverter.convertFromGoogleFile(file, account, folderId)); + savedFile = fileRepository.save(FileConverter.convertFromGoogleFile(file, account, folderId)); } else if (mimeType.contains("video")) { + if (file.getHasThumbnail()) { + savePreview(file); + } address = NotificationDto.Address.VIDEO; - fileRepository.save(FileConverter.convertFromGoogleFile(file, account, folderId)); + savedFile = fileRepository.save(FileConverter.convertFromGoogleFile(file, account, folderId)); } else { return; } - if (Arrays.asList(env.getActiveProfiles()).contains("prod")) { - setWatchChannel(new NotificationDto(address), file.getId(), account); + if (Arrays.asList(env.getActiveProfiles()).contains("prod") && + savedFile.isNeedChannel()) { + Channel channel = setWatchChannel(new NotificationDto(address), file.getId(), account); + savedFile.setChannel(channel); + fileRepository.save(savedFile); + } else if (mimeType.contains("video") && savedFile.getChannel() != null) { + deleteWatchChannel(savedFile.getChannel(), account); + savedFile.setChannel(null); + savedFile.setNeedChannel(false); } } - private void setWatchChannel(NotificationDto notificationDto, String id, Account account) { + private Channel setWatchChannel(NotificationDto notificationDto, String id, Account account) { final String url = "https://www.googleapis.com/drive/v3/files/{fileID}/watch"; - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.setContentType(MediaType.APPLICATION_JSON); - httpHeaders.setBearerAuth(account.getToken()); + HttpHeaders httpHeaders = createHeader(account); HttpEntity<NotificationDto> entity = new HttpEntity<>(notificationDto, httpHeaders); LOG.info("Request: {}, {}, {}", id, entity.getBody(), entity.getHeaders()); - String response = restTemplate.postForObject(url, entity, String.class, id); + Channel channel = restTemplate.postForObject(url, entity, Channel.class, id); + LOG.info(String.valueOf(channel)); + return channel; + } + + private void deleteWatchChannel(Channel channel, Account account) { + final String url = "https://www.googleapis.com/drive/v3/channels/stop"; + HttpHeaders httpHeaders = createHeader(account); + + HttpEntity<Channel> entity = new HttpEntity<>(channel, httpHeaders); + LOG.info("Request: {}, {}", entity.getBody(), entity.getHeaders()); + String response = restTemplate.postForObject(url, entity, String.class); LOG.info(response); } + public HttpHeaders createHeader(Account account) { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + httpHeaders.setBearerAuth(account.getToken()); + return httpHeaders; + } + private void savePreview(com.google.api.services.drive.model.File file) { try { BufferedInputStream in = diff --git a/src/main/java/com/rudakova/media_archive/model/Channel.java b/src/main/java/com/rudakova/media_archive/model/Channel.java new file mode 100644 index 0000000000000000000000000000000000000000..41ad97fb683b80fca755ac8d2d3455384df7d128 --- /dev/null +++ b/src/main/java/com/rudakova/media_archive/model/Channel.java @@ -0,0 +1,65 @@ +package com.rudakova.media_archive.model; + +public class Channel { + + private String kind; + + private String id; + + private String resourceId; + + private String resourceUri; + + private long expiration; + + public String getKind() { + return kind; + } + + public void setKind(String kind) { + this.kind = kind; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getResourceId() { + return resourceId; + } + + public void setResourceId(String resourceId) { + this.resourceId = resourceId; + } + + public String getResourceUri() { + return resourceUri; + } + + public void setResourceUri(String resourceUri) { + this.resourceUri = resourceUri; + } + + public long getExpiration() { + return expiration; + } + + public void setExpiration(long expiration) { + this.expiration = expiration; + } + + @Override + public String toString() { + return "Channel{" + + "kind='" + kind + '\'' + + ", id='" + id + '\'' + + ", resourceId='" + resourceId + '\'' + + ", resourceUri='" + resourceUri + '\'' + + ", expiration=" + expiration + + '}'; + } +} diff --git a/src/main/java/com/rudakova/media_archive/model/File.java b/src/main/java/com/rudakova/media_archive/model/File.java index 25daba476adb869448e65caf39c7f90c446f3f7d..31c4c4e176adfcdda8019fcf29f9e433b9ed0936 100644 --- a/src/main/java/com/rudakova/media_archive/model/File.java +++ b/src/main/java/com/rudakova/media_archive/model/File.java @@ -3,7 +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; import java.util.HashMap; import java.util.List; @@ -33,6 +32,10 @@ public class File { private String preview; + private Channel channel; + + private boolean needChannel = true; + public String getId() { return id; } @@ -120,4 +123,20 @@ public class File { public void setPreview(String preview) { this.preview = preview; } + + public Channel getChannel() { + return channel; + } + + public void setChannel(Channel channel) { + this.channel = channel; + } + + public boolean isNeedChannel() { + return needChannel; + } + + public void setNeedChannel(boolean needChannel) { + this.needChannel = needChannel; + } } diff --git a/target/MediaArchive-1.0-SNAPSHOT.jar b/target/MediaArchive-1.0-SNAPSHOT.jar index ad5724dfe4122c89411fe56d7493d7d7b38d2579..af69eaae9863c482a6999c741292ef12ec7e1afc 100644 Binary files a/target/MediaArchive-1.0-SNAPSHOT.jar and b/target/MediaArchive-1.0-SNAPSHOT.jar differ