Upgrade dependencies (#2468)

This commit is contained in:
Lai Jiang
2024-06-05 15:50:42 +00:00
committed by GitHub
parent 61eee45ad0
commit 49cb1875d1
23 changed files with 326 additions and 300 deletions
@@ -401,7 +401,7 @@ public class CloudDnsWriter extends BaseDnsWriter {
if (err == null || err.getErrors().size() > 1) {
throw e;
}
String errorReason = err.getErrors().get(0).getReason();
String errorReason = err.getErrors().getFirst().getReason();
if (RETRYABLE_EXCEPTION_REASONS.contains(errorReason)) {
throw new ZoneStateException(errorReason);
@@ -16,11 +16,9 @@ package google.registry.storage.drive;
import com.google.api.client.http.ByteArrayContent;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Children;
import com.google.api.services.drive.model.ChildList;
import com.google.api.services.drive.model.ChildReference;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.ParentReference;
import com.google.api.services.drive.model.FileList;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.net.MediaType;
@@ -37,16 +35,19 @@ public class DriveConnection {
/** Drive client instance wrapped by this class. */
@Inject Drive drive;
@Inject public DriveConnection() {}
@Inject
public DriveConnection() {}
/**
* Creates a folder with the given parent.
*
* @return the folder id.
*/
public String createFolder(String title, String parentFolderId) throws IOException {
return drive.files()
.insert(createFileReference(title, GOOGLE_FOLDER, parentFolderId))
public String createFolder(String name, @Nullable String parentFolderId) throws IOException {
return drive
.files()
.create(createFileReference(name, GOOGLE_FOLDER, parentFolderId))
.execute()
.getId();
}
@@ -60,11 +61,12 @@ public class DriveConnection {
*
* @return the file id.
*/
public String createFile(String title, MediaType mimeType, String parentFolderId, byte[] bytes)
public String createFile(String name, MediaType mimeType, String parentFolderId, byte[] bytes)
throws IOException {
return drive.files()
.insert(
createFileReference(title, mimeType, parentFolderId),
return drive
.files()
.create(
createFileReference(name, mimeType, parentFolderId),
new ByteArrayContent(mimeType.toString(), bytes))
.execute()
.getId();
@@ -72,79 +74,79 @@ public class DriveConnection {
/**
* Creates a file with the given parent or updates the existing one if a file already exists with
* that same title and parent.
* that same name and parent.
*
* @throws IllegalStateException if multiple files with that name exist in the given folder.
* @throws IOException if communication with Google Drive fails for any reason.
* @return the file id.
*/
public String createOrUpdateFile(
String title, MediaType mimeType, String parentFolderId, byte[] bytes) throws IOException {
List<String> existingFiles = listFiles(parentFolderId, String.format("title = '%s'", title));
String name, MediaType mimeType, String parentFolderId, byte[] bytes) throws IOException {
List<String> existingFiles = listFiles(parentFolderId, String.format("name = '%s'", name));
if (existingFiles.size() > 1) {
throw new IllegalStateException(String.format(
"Could not update file '%s' in Drive folder id '%s' because multiple files with that "
+ "name already exist.",
title,
parentFolderId));
throw new IllegalStateException(
String.format(
"Could not update file '%s' in Drive folder id '%s' because multiple files with that "
+ "name already exist.",
name, parentFolderId));
}
return existingFiles.isEmpty()
? createFile(title, mimeType, parentFolderId, bytes)
: updateFile(existingFiles.get(0), title, mimeType, bytes);
? createFile(name, mimeType, parentFolderId, bytes)
: updateFile(existingFiles.getFirst(), name, mimeType, bytes);
}
/**
* Updates the file with the given id in place, setting the title, content, and mime type to the
* Updates the file with the given id in place, setting the name, content, and mime type to the
* newly specified values.
*
* @return the file id.
*/
public String updateFile(String fileId, String title, MediaType mimeType, byte[] bytes)
public String updateFile(String fileId, String name, MediaType mimeType, byte[] bytes)
throws IOException {
File file = new File().setTitle(title);
return drive.files()
File file = new File().setName(name);
return drive
.files()
.update(fileId, file, new ByteArrayContent(mimeType.toString(), bytes))
.execute()
.getId();
}
/**
* Returns a list of Drive file ids for all files in Google Drive in the folder with the
* specified id.
* Returns a list of Drive file ids for all files in Google Drive in the folder with the specified
* id.
*/
public List<String> listFiles(String parentFolderId) throws IOException {
return listFiles(parentFolderId, null);
}
/**
* Returns a list of Drive file ids for all files in Google Drive in the folder with the
* specified id and matching the given Drive query.
* Returns a list of Drive file ids for all files in Google Drive in the folder with the specified
* id and matching the given Drive query.
*
* @see <a href="https://developers.google.com/drive/web/search-parameters">The query format</a>
*/
public List<String> listFiles(String parentFolderId, String query) throws IOException {
ImmutableList.Builder<String> result = new ImmutableList.Builder<>();
Children.List req = drive.children().list(parentFolderId);
Files.List req = drive.files().list();
StringBuilder q = new StringBuilder(String.format("'%s' in parents", parentFolderId));
if (!Strings.isNullOrEmpty(query)) {
req.setQ(query);
q.append(String.format(" and %s", query));
}
req.setQ(q.toString());
do {
ChildList files = req.execute();
for (ChildReference child : files.getItems()) {
result.add(child.getId());
}
FileList files = req.execute();
files.getFiles().forEach(file -> result.add(file.getId()));
req.setPageToken(files.getNextPageToken());
} while (!Strings.isNullOrEmpty(req.getPageToken()));
return result.build();
}
/** Constructs an object representing a file (or folder) with a given title and parent. */
/** Constructs an object representing a file (or folder) with a given name and parent. */
private File createFileReference(
String title, MediaType mimeType, @Nullable String parentFolderId) {
String name, MediaType mimeType, @Nullable String parentFolderId) {
return new File()
.setTitle(title)
.setName(name)
.setMimeType(mimeType.toString())
.setParents(parentFolderId == null
? null
: ImmutableList.of(new ParentReference().setId(parentFolderId)));
.setParents(parentFolderId == null ? null : ImmutableList.of(parentFolderId));
}
}