Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#312 - handle empty files upload #517

Merged
merged 5 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 9 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,9 @@
package com.crowdin.cli.client;


andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
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(WARNING.withIcon(String.format(RESOURCE_BUNDLE.getString("message.uploading_file_skipped"), fileFullPath)));
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
return;
} catch (Exception e) {
errorsPresented.set(true);
throw new RuntimeException(
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ error.cast_param_list_type='%s' list contains value(s) of type '%s' instead of '
error.dest_and_pattern_in_source=The 'dest' parameter only works for single files specified in the 'source' parameter
error.dest_and_preserve_hierarchy=The 'dest' parameter only works for single files with the specified 'preserve_hierarchy': true option
error.upload_to_storage=Failed to upload the '%s' file to the storage. Please contact our support team for help
error.empty_file_upload=Wrong parameters: %s
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
error.language_not_exist=Language '%s' doesn't exist in the project. Try specifying another language code
error.languages_not_exist=Language(s) %s doesn't exist in the project. Try specifying another language code(s)
error.building_translation=Failed to build translation. Please contact our support team for help
Expand Down Expand Up @@ -397,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