Skip to content

Commit

Permalink
#312 - handle empty files upload (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
yzerk authored Jan 11, 2023
1 parent c10d4a3 commit 65aea63
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,12 @@ public List<Branch> listBranches() {
}

@Override
public Long uploadStorage(String fileName, InputStream content) {
Storage storage = executeRequest(() -> this.client.getStorageApi()
public Long uploadStorage(String fileName, InputStream content) throws ResponseException {
Map<BiPredicate<String, String>, ResponseException> errorHandlers = new LinkedHashMap<BiPredicate<String, String>, ResponseException>() {{
put((code, message) -> StringUtils.containsAny(message, "streamIsEmpty", "Stream size is null. Not empty content expected"),
new EmptyFileException("Not empty content expected"));
}};
Storage storage = executeRequest(errorHandlers, () -> this.client.getStorageApi()
.addStorage(fileName, content)
.getData());
return storage.getId();
Expand Down Expand Up @@ -187,6 +191,7 @@ public void addSource(AddFileRequest request) throws ResponseException {
put((code, message) -> message.contains("File from storage with id #" + request.getStorageId() + " was not found"), new RepeatException());
put((code, message) -> StringUtils.contains(message, "Name must be unique"), new ExistsResponseException());
put((code, message) -> StringUtils.contains(message, "Invalid SRX specified"), new ResponseException("Invalid SRX file specified"));
put((code, message) -> StringUtils.containsAny(message, "isEmpty", "Value is required and can't be empty"), new EmptyFileException("Value is required and can't be empty"));
}};
executeRequestWithPossibleRetry(
errorHandlers,
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/crowdin/cli/client/EmptyFileException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.crowdin.cli.client;

public class EmptyFileException extends ResponseException {

public EmptyFileException(String message) {
super(message);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/crowdin/cli/client/ProjectClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface ProjectClient extends Client {

List<Branch> listBranches();

Long uploadStorage(String fileName, InputStream content);
Long uploadStorage(String fileName, InputStream content) throws ResponseException;

Directory addDirectory(AddDirectoryRequest request) throws ResponseException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.crowdin.cli.commands.actions;

import com.crowdin.cli.client.CrowdinProjectFull;
import com.crowdin.cli.client.EmptyFileException;
import com.crowdin.cli.client.ExistsResponseException;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.commands.NewAction;
Expand Down Expand Up @@ -259,6 +260,10 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {

try (InputStream fileStream = new FileInputStream(sourceFile)) {
request.setStorageId(client.uploadStorage(source.substring(source.lastIndexOf(Utils.PATH_SEPARATOR) + 1), fileStream));
} catch (EmptyFileException e){
errorsPresented.set(false);
out.println(SKIPPED.withIcon(String.format(RESOURCE_BUNDLE.getString("message.uploading_file_skipped"), fileFullPath)));
return;
} catch (Exception e) {
errorsPresented.set(true);
throw new RuntimeException(
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ message.new_version_text=New version of Crowdin CLI is available! %s -> %s
message.new_version_text.2=Changelog: @|cyan https://github.com/crowdin/crowdin-cli/releases/latest|@
message.new_version_text.3=Please update for the best experience!
message.uploading_file=File @|bold '%s'|@
message.uploading_file_skipped=File @|bold '%s'|@ was skipped since it is empty
message.downloaded_file=File @|bold '%s'|@
message.translation_file=Translation file @|bold '%s'|@
message.build_language_archive=Building ZIP archive with the latest translations for @|bold '%s'|@
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public void testAddBranch() {
}

@Test
public void testUploadStorage() throws IOException {
public void testUploadStorage() throws IOException, ResponseException {
InputStream requestData = IOUtils.toInputStream("Something to send", "UTF-8");
StorageResponseObject response = new StorageResponseObject() {{
setData(new Storage());
Expand Down

0 comments on commit 65aea63

Please sign in to comment.