-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Gutenberg - async media upload Post background processing #9132
Changes from 2 commits
abce82c
9ed2664
7eb0760
75ddb4b
144c93c
c362bc8
89fe3ec
625e9a8
f797b98
6c5ec0a
924725e
a321f5e
7472379
0fc2aa4
2cdf852
cd3d599
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
import org.wordpress.android.util.AppLog; | ||
import org.wordpress.android.util.PermissionUtils; | ||
import org.wordpress.android.util.ProfilingUtils; | ||
import org.wordpress.android.util.StringUtils; | ||
import org.wordpress.android.util.ToastUtils; | ||
import org.wordpress.android.util.AppLog.T; | ||
import org.wordpress.android.util.helpers.MediaFile; | ||
|
@@ -39,6 +40,8 @@ | |
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class GutenbergEditorFragment extends EditorFragmentAbstract implements | ||
View.OnTouchListener, | ||
|
@@ -364,6 +367,57 @@ public static boolean contentContainsGutenbergBlocks(String postContent) { | |
return (postContent != null && postContent.contains(GUTENBERG_BLOCK_START)); | ||
} | ||
|
||
public static String replaceMediaFileWithUrl(Context context, @NonNull String postContent, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this is a static method, maybe it's a good candidate for some Util or Helper? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good idea, I was following the same pattern as we had for Aztec: in Aztec the class that can handle / is responsible for editing a Post is AztecEditorFragment, and we needed a static method as this manipulation of the Post's content happens asynchronously (that is, when the AztecEditorFragment is not instanced). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressed this change in 7472379 |
||
String localMediaId, MediaFile mediaFile) { | ||
if (mediaFile != null) { | ||
String remoteUrl = StringUtils.notNullStr(Utils.escapeQuotes(mediaFile.getFileURL())); | ||
// TODO: replace the URL | ||
if (!mediaFile.isVideo()) { | ||
// replace gutenberg block id holder with serverMediaId, and url_holder with remoteUrl | ||
String oldImgBlockHeader = String.format("<!-- wp:image {\"id\":%s} -->", localMediaId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably we can put this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressed in a321f5e |
||
String newImgBlockHeader = String.format("<!-- wp:image {\"id\":%s} -->", mediaFile.getMediaId()); | ||
postContent = postContent.replace(oldImgBlockHeader, newImgBlockHeader); | ||
|
||
// replace class wp-image-id with serverMediaId, and url_holder with remoteUrl | ||
String oldImgClass = String.format("class=\"wp-image-%s\"", localMediaId); | ||
String newImgClass = String.format("class=\"wp-image-%s\"", mediaFile.getMediaId()); | ||
postContent = postContent.replace(oldImgClass, newImgClass); | ||
|
||
// let's first find this occurrence and keep note of the position, as we need to replace the | ||
// immediate `src` value before | ||
int iStartOfWpImageClassAttribute = postContent.indexOf(newImgClass); | ||
if (iStartOfWpImageClassAttribute != -1) { | ||
// now search negatively, for the src attribute appearing right before | ||
int iStartOfImgTag = postContent.lastIndexOf("<img", iStartOfWpImageClassAttribute); | ||
if (iStartOfImgTag != -1) { | ||
Pattern p = Pattern.compile("<img[^>]*src=[\\\"']([^\\\"^']*)"); | ||
Matcher m = p.matcher(postContent.substring(iStartOfImgTag)); | ||
if (m.find()) { | ||
String src = m.group(); | ||
int startIndex = src.indexOf("src=") + 5; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we put magic number 5 in some variable so that we can remember what's the idea behind it ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 addressed in 6c5ec0a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. er, re-wrote it in 924725e to a class constant so it's easier to read |
||
String srcTag = src.substring(startIndex, src.length()); | ||
// now replace the url | ||
postContent = postContent.replace(srcTag, remoteUrl); | ||
} | ||
} | ||
} | ||
} else { | ||
// TODO replace in GB Video block? | ||
} | ||
// re-set the post content | ||
//postContent = toHtml(builder, parser); | ||
} | ||
return postContent; | ||
} | ||
|
||
public static boolean isMediaInPostBody(Context context, @NonNull String postContent, | ||
String localMediaId) { | ||
// check if media is in Gutenberg Post | ||
String imgBlockHeaderToSearchFor = String.format("<!-- wp:image {\"id\":%s} -->", localMediaId); | ||
return postContent.indexOf(imgBlockHeaderToSearchFor) != -1; | ||
} | ||
|
||
|
||
/* | ||
* TODO: REMOVE THIS ONCE AZTEC COMPLETELY REPLACES THE VISUAL EDITOR IN WPANDROID APP | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This // TODO mark left here on purpose