Skip to content

Commit

Permalink
Merge pull request #20495 from wordpress-mobile/issue/20494
Browse files Browse the repository at this point in the history
Fixes: NumberFormatExcpetion in BlockProcessor
  • Loading branch information
pantstamp authored Mar 20, 2024
2 parents 27ad674 + 2e17d61 commit ae393b7
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AudioBlockProcessor(localId: String?, mediaFile: MediaFile?) : BlockProces

return if (id != null && !id.isJsonNull && id.asString == mLocalId) {
jsonAttributes.apply {
addProperty(ID_ATTRIBUTE, Integer.parseInt(mRemoteId))
addIntPropertySafely(this, ID_ATTRIBUTE, mRemoteId)
}
true
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Document.OutputSettings;
import org.wordpress.android.editor.Utils;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.helpers.MediaFile;

import java.util.regex.Matcher;

import static org.wordpress.android.ui.posts.mediauploadcompletionprocessors.MediaUploadCompletionProcessorPatterns.PATTERN_BLOCK_CAPTURES;
import static org.wordpress.android.ui.posts.mediauploadcompletionprocessors.MediaUploadCompletionProcessorPatterns.PATTERN_SELF_CLOSING_BLOCK_CAPTURES;
import static org.wordpress.android.util.AppLog.T.MEDIA;

/**
* Abstract class to be extended for each enumerated {@link MediaBlockType}.
Expand Down Expand Up @@ -129,6 +131,14 @@ String processBlock(String block) {
return processBlock(block, false);
}

final void addIntPropertySafely(JsonObject jsonAttributes, String propertyName, String value) {
try {
jsonAttributes.addProperty(propertyName, Integer.parseInt(value));
} catch (NumberFormatException e) {
AppLog.e(MEDIA, e.getMessage());
}
}

/**
* All concrete implementations must implement this method for the particular block type. The document represents
* the html contents of the block to be processed, and is to be mutated in place.<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public CoverBlockProcessor(String localId, MediaFile mediaFile,
@Override boolean processBlockJsonAttributes(JsonObject jsonAttributes) {
JsonElement id = jsonAttributes.get("id");
if (id != null && !id.isJsonNull() && id.getAsInt() == Integer.parseInt(mLocalId, 10)) {
jsonAttributes.addProperty("id", Integer.parseInt(mRemoteId, 10));
addIntPropertySafely(jsonAttributes, "id", mRemoteId);

jsonAttributes.addProperty("url", mRemoteUrl);

// check if background type is video
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.helpers.MediaFile;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.wordpress.android.util.AppLog.T.MEDIA;

public class GalleryBlockProcessor extends BlockProcessor {
private final MediaUploadCompletionProcessor mMediaUploadCompletionProcessor;
private String mAttachmentPageUrl;
Expand Down Expand Up @@ -93,7 +96,11 @@ public GalleryBlockProcessor(String localId, MediaFile mediaFile, String siteUrl
for (int i = 0; i < ids.size(); i++) {
JsonElement id = ids.get(i);
if (id != null && !id.isJsonNull() && id.getAsString().equals(mLocalId)) {
ids.set(i, new JsonPrimitive(Integer.parseInt(mRemoteId, 10)));
try {
ids.set(i, new JsonPrimitive(Integer.parseInt(mRemoteId, 10)));
} catch (NumberFormatException e) {
AppLog.e(MEDIA, e.getMessage());
}
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.helpers.MediaFile;

import static org.wordpress.android.util.AppLog.T.MEDIA;

public class ImageBlockProcessor extends BlockProcessor {
public ImageBlockProcessor(String localId, MediaFile mediaFile) {
Expand Down Expand Up @@ -37,14 +35,9 @@ public ImageBlockProcessor(String localId, MediaFile mediaFile) {
@Override boolean processBlockJsonAttributes(JsonObject jsonAttributes) {
JsonElement id = jsonAttributes.get("id");
if (id != null && !id.isJsonNull() && id.getAsString().equals(mLocalId)) {
try {
jsonAttributes.addProperty("id", Integer.parseInt(mRemoteId));
} catch (NumberFormatException e) {
AppLog.e(MEDIA, e.getMessage());
}
addIntPropertySafely(jsonAttributes, "id", mRemoteId);
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public MediaTextBlockProcessor(String localId, MediaFile mediaFile) {
@Override boolean processBlockJsonAttributes(JsonObject jsonAttributes) {
JsonElement id = jsonAttributes.get("mediaId");
if (id != null && !id.isJsonNull() && id.getAsString().equals(mLocalId)) {
jsonAttributes.addProperty("mediaId", Integer.parseInt(mRemoteId));
addIntPropertySafely(jsonAttributes, "mediaId", mRemoteId);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ public VideoBlockProcessor(String localId, MediaFile mediaFile) {
@Override boolean processBlockJsonAttributes(JsonObject jsonAttributes) {
JsonElement id = jsonAttributes.get("id");
if (id != null && !id.isJsonNull() && id.getAsString().equals(mLocalId)) {
jsonAttributes.addProperty("id", Integer.parseInt(mRemoteId));
addIntPropertySafely(jsonAttributes, "id", mRemoteId);
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class VideoPressBlockProcessor(

return if (id != null && !id.isJsonNull && id.asString == mLocalId) {
jsonAttributes.apply {
addProperty(ID_ATTRIBUTE, Integer.parseInt(mRemoteId))
addIntPropertySafely(this, ID_ATTRIBUTE, mRemoteId)
addProperty(GUID_ATTRIBUTE, mRemoteGuid)
if (src?.startsWith("file:") == true) {
remove(SRC_ATTRIBUTE)
Expand Down

0 comments on commit ae393b7

Please sign in to comment.