Add backend to upload images.

Also added an Image upload button for comments.
This commit is contained in:
Balazs Toldi 2023-07-27 18:32:44 +02:00
parent 2fc4020b72
commit 919d259f61
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
12 changed files with 72 additions and 196 deletions

View File

@ -12,13 +12,19 @@ public class RetrofitHolder {
private Retrofit retrofit;
private OkHttpClient okHttpClient;
private String baseURL = APIUtils.API_BASE_URI;
public Retrofit getRetrofit() {
return retrofit;
}
public void setBaseURL(String baseURL){
retrofit = createRetrofit(okHttpClient,baseURL);
public void setBaseURL(String baseURL) {
retrofit = createRetrofit(okHttpClient, baseURL);
this.baseURL = baseURL;
}
public String getBaseURL() {
return baseURL;
}
public RetrofitHolder(OkHttpClient okHttpClient) {

View File

@ -39,17 +39,10 @@ import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
import eu.toldi.infinityforlemmy.RetrofitHolder;
import io.noties.markwon.AbstractMarkwonPlugin;
import io.noties.markwon.Markwon;
import io.noties.markwon.MarkwonConfiguration;
import io.noties.markwon.MarkwonPlugin;
import io.noties.markwon.core.MarkwonTheme;
import io.noties.markwon.recycler.MarkwonAdapter;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import eu.toldi.infinityforlemmy.Infinity;
import eu.toldi.infinityforlemmy.R;
import eu.toldi.infinityforlemmy.RedditDataRoomDatabase;
import eu.toldi.infinityforlemmy.RetrofitHolder;
import eu.toldi.infinityforlemmy.UploadImageEnabledActivity;
import eu.toldi.infinityforlemmy.UploadedImage;
import eu.toldi.infinityforlemmy.account.Account;
@ -66,6 +59,13 @@ import eu.toldi.infinityforlemmy.events.SwitchAccountEvent;
import eu.toldi.infinityforlemmy.markdown.MarkdownUtils;
import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils;
import eu.toldi.infinityforlemmy.utils.Utils;
import io.noties.markwon.AbstractMarkwonPlugin;
import io.noties.markwon.Markwon;
import io.noties.markwon.MarkwonConfiguration;
import io.noties.markwon.MarkwonPlugin;
import io.noties.markwon.core.MarkwonTheme;
import io.noties.markwon.recycler.MarkwonAdapter;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import retrofit2.Retrofit;
public class CommentActivity extends BaseActivity implements UploadImageEnabledActivity, AccountChooserBottomSheetFragment.AccountChooserListener {
@ -449,10 +449,10 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA
Toast.makeText(CommentActivity.this, R.string.error_getting_image, Toast.LENGTH_LONG).show();
return;
}
Utils.uploadImageToReddit(this, mExecutor, mRetrofit.getRetrofit(), mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, mRetrofit,
mAccessToken, binding.commentCommentEditText, binding.commentCoordinatorLayout, data.getData(), uploadedImages);
} else if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) {
Utils.uploadImageToReddit(this, mExecutor, mRetrofit.getRetrofit(), mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, mRetrofit,
mAccessToken, binding.commentCommentEditText, binding.commentCoordinatorLayout, capturedImageUri, uploadedImages);
} else if (requestCode == MARKDOWN_PREVIEW_REQUEST_CODE) {
sendComment(mMenu == null ? null : mMenu.findItem(R.id.action_send_comment_activity));

View File

@ -275,10 +275,10 @@ public class EditCommentActivity extends BaseActivity implements UploadImageEnab
Toast.makeText(EditCommentActivity.this, R.string.error_getting_image, Toast.LENGTH_LONG).show();
return;
}
Utils.uploadImageToReddit(this, mExecutor, retrofit.getRetrofit(), mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, retrofit,
mAccessToken, contentEditText, coordinatorLayout, data.getData(), uploadedImages);
} else if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) {
Utils.uploadImageToReddit(this, mExecutor, retrofit.getRetrofit(), mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, retrofit,
mAccessToken, contentEditText, coordinatorLayout, capturedImageUri, uploadedImages);
} else if (requestCode == MARKDOWN_PREVIEW_REQUEST_CODE) {
editComment();

View File

@ -44,6 +44,7 @@ import butterknife.BindView;
import butterknife.ButterKnife;
import eu.toldi.infinityforlemmy.Infinity;
import eu.toldi.infinityforlemmy.R;
import eu.toldi.infinityforlemmy.RetrofitHolder;
import eu.toldi.infinityforlemmy.UploadImageEnabledActivity;
import eu.toldi.infinityforlemmy.UploadedImage;
import eu.toldi.infinityforlemmy.adapters.MarkdownBottomBarRecyclerViewAdapter;
@ -88,8 +89,8 @@ public class EditPostActivity extends BaseActivity implements UploadImageEnabled
@BindView(R.id.markdown_bottom_bar_recycler_view_edit_post_activity)
RecyclerView markdownBottomBarRecyclerView;
@Inject
@Named("oauth")
Retrofit mOauthRetrofit;
@Named("no_oauth")
RetrofitHolder mRetrofit;
@Inject
@Named("upload_media")
Retrofit mUploadMediaRetrofit;
@ -241,7 +242,7 @@ public class EditPostActivity extends BaseActivity implements UploadImageEnabled
params.put(APIUtils.THING_ID_KEY, mFullName);
params.put(APIUtils.TEXT_KEY, contentEditText.getText().toString());
mOauthRetrofit.create(RedditAPI.class)
mRetrofit.getRetrofit().create(RedditAPI.class)
.editPostOrComment(APIUtils.getOAuthHeader(mAccessToken), params)
.enqueue(new Callback<String>() {
@Override
@ -272,10 +273,10 @@ public class EditPostActivity extends BaseActivity implements UploadImageEnabled
Toast.makeText(EditPostActivity.this, R.string.error_getting_image, Toast.LENGTH_LONG).show();
return;
}
Utils.uploadImageToReddit(this, mExecutor, mOauthRetrofit, mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, mRetrofit,
mAccessToken, contentEditText, coordinatorLayout, data.getData(), uploadedImages);
} else if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) {
Utils.uploadImageToReddit(this, mExecutor, mOauthRetrofit, mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, mRetrofit,
mAccessToken, contentEditText, coordinatorLayout, capturedImageUri, uploadedImages);
} else if (requestCode == MARKDOWN_PREVIEW_REQUEST_CODE) {
editPost();

View File

@ -492,7 +492,7 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee
mExecutor.execute(() -> {
try {
Bitmap resource = Glide.with(PostGalleryActivity.this).asBitmap().load(imageUri).submit().get();
String response = UploadImageUtils.uploadImage(mOauthRetrofit, mUploadMediaRetrofit, mAccessToken, resource, true);
String response = UploadImageUtils.uploadImage(mRetrofit, mAccessToken, resource, true);
String mediaId = new JSONObject(response).getJSONObject(JSONUtils.ASSET_KEY).getString(JSONUtils.ASSET_ID_KEY);
handler.post(() -> {
adapter.setImageAsUploaded(mediaId);

View File

@ -637,10 +637,10 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr
Toast.makeText(PostTextActivity.this, R.string.error_getting_image, Toast.LENGTH_LONG).show();
return;
}
Utils.uploadImageToReddit(this, mExecutor, mOauthRetrofit, mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, mRetrofit,
mAccessToken, contentEditText, coordinatorLayout, data.getData(), uploadedImages);
} else if (requestCode == CAPTURE_IMAGE_REQUEST_CODE) {
Utils.uploadImageToReddit(this, mExecutor, mOauthRetrofit, mUploadMediaRetrofit,
Utils.uploadImageToReddit(this, mExecutor, mRetrofit,
mAccessToken, contentEditText, coordinatorLayout, capturedImageUri, uploadedImages);
} else if (requestCode == MARKDOWN_PREVIEW_REQUEST_CODE) {
submitPost(mMenu.findItem(R.id.action_send_post_text_activity));

View File

@ -31,7 +31,7 @@ public class MarkdownBottomBarRecyclerViewAdapter extends RecyclerView.Adapter<R
public static final int CODE_BLOCK = 9;
public static final int UPLOAD_IMAGE = 10;
private static final int ITEM_COUNT = 10;
private static final int ITEM_COUNT = 11;
private CustomThemeWrapper customThemeWrapper;
private ItemClickListener itemClickListener;

View File

@ -10,13 +10,17 @@ import eu.toldi.infinityforlemmy.dto.FollowCommunityDTO;
import eu.toldi.infinityforlemmy.dto.PostVoteDTO;
import eu.toldi.infinityforlemmy.dto.SaveCommentDTO;
import eu.toldi.infinityforlemmy.dto.SavePostDTO;
import okhttp3.MultipartBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Part;
import retrofit2.http.Query;
public interface LemmyAPI {
@ -133,4 +137,11 @@ public interface LemmyAPI {
@Headers("Content-Type: application/json")
@PUT("api/v3/comment")
Call<String> commentEdit(@Body EditCommentDTO params);
@Multipart
@POST("/pictrs/image")
Call<String> uploadImage(
@Header("Cookie") String token,
@Part MultipartBody.Part filePart
);
}

View File

@ -10,20 +10,17 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParserException;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import eu.toldi.infinityforlemmy.Flair;
import eu.toldi.infinityforlemmy.RetrofitHolder;
import eu.toldi.infinityforlemmy.apis.RedditAPI;
import eu.toldi.infinityforlemmy.utils.APIUtils;
import eu.toldi.infinityforlemmy.utils.JSONUtils;
import eu.toldi.infinityforlemmy.utils.UploadImageUtils;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
@ -38,14 +35,14 @@ public class SubmitPost {
flair, isSpoiler, isNSFW, receivePostReplyNotifications, kind, null, submitPostListener);
}
public static void submitImagePost(Executor executor, Handler handler, Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
public static void submitImagePost(Executor executor, Handler handler, RetrofitHolder mRetrofit,
String accessToken, String subredditName, String title, Bitmap image,
Flair flair, boolean isSpoiler, boolean isNSFW,
boolean receivePostReplyNotifications, SubmitPostListener submitPostListener) {
try {
String imageUrlOrError = UploadImageUtils.uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, image);
String imageUrlOrError = UploadImageUtils.uploadImage(mRetrofit, accessToken, image);
if (imageUrlOrError != null && !imageUrlOrError.startsWith("Error: ")) {
submitPost(executor, handler, oauthRetrofit, accessToken,
submitPost(executor, handler, mRetrofit.getRetrofit(), accessToken,
subredditName, title, imageUrlOrError, flair, isSpoiler, isNSFW,
receivePostReplyNotifications, APIUtils.KIND_IMAGE, null, submitPostListener);
} else {
@ -57,70 +54,6 @@ public class SubmitPost {
}
}
public static void submitVideoPost(Executor executor, Handler handler, Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
Retrofit uploadVideoRetrofit, String accessToken,
String subredditName, String title, File buffer, String mimeType,
Bitmap posterBitmap, Flair flair, boolean isSpoiler, boolean isNSFW,
boolean receivePostReplyNotifications, SubmitPostListener submitPostListener) {
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
String fileType = mimeType.substring(mimeType.indexOf("/") + 1);
Map<String, String> uploadImageParams = new HashMap<>();
uploadImageParams.put(APIUtils.FILEPATH_KEY, "post_video." + fileType);
uploadImageParams.put(APIUtils.MIMETYPE_KEY, mimeType);
Call<String> uploadImageCall = api.uploadImage(APIUtils.getOAuthHeader(accessToken), uploadImageParams);
try {
Response<String> uploadImageResponse = uploadImageCall.execute();
if (uploadImageResponse.isSuccessful()) {
Map<String, RequestBody> nameValuePairsMap = UploadImageUtils.parseJSONResponseFromAWS(uploadImageResponse.body());
RequestBody fileBody = RequestBody.create(buffer, MediaType.parse("application/octet-stream"));
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", "post_video." + fileType, fileBody);
RedditAPI uploadVideoToAWSApi;
if (fileType.equals("gif")) {
uploadVideoToAWSApi = uploadMediaRetrofit.create(RedditAPI.class);
} else {
uploadVideoToAWSApi = uploadVideoRetrofit.create(RedditAPI.class);
}
Call<String> uploadMediaToAWS = uploadVideoToAWSApi.uploadMediaToAWS(nameValuePairsMap, fileToUpload);
Response<String> uploadMediaToAWSResponse = uploadMediaToAWS.execute();
if (uploadMediaToAWSResponse.isSuccessful()) {
String url = UploadImageUtils.parseXMLResponseFromAWS(uploadMediaToAWSResponse.body());
if (url == null) {
submitPostListener.submitFailed(null);
return;
}
String imageUrlOrError = UploadImageUtils.uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, posterBitmap);
if (imageUrlOrError != null && !imageUrlOrError.startsWith("Error: ")) {
if (fileType.equals("gif")) {
submitPost(executor, handler, oauthRetrofit, accessToken,
subredditName, title, url, flair, isSpoiler, isNSFW,
receivePostReplyNotifications, APIUtils.KIND_VIDEOGIF, imageUrlOrError,
submitPostListener);
} else {
submitPost(executor, handler, oauthRetrofit, accessToken,
subredditName, title, url, flair, isSpoiler, isNSFW,
receivePostReplyNotifications, APIUtils.KIND_VIDEO, imageUrlOrError,
submitPostListener);
}
} else {
submitPostListener.submitFailed(imageUrlOrError);
}
} else {
submitPostListener.submitFailed(uploadMediaToAWSResponse.code() + " " + uploadMediaToAWSResponse.message());
}
} else {
submitPostListener.submitFailed(uploadImageResponse.code() + " " + uploadImageResponse.message());
}
} catch (IOException | XmlPullParserException | JSONException e) {
e.printStackTrace();
submitPostListener.submitFailed(e.getMessage());
}
}
public static void submitCrosspost(Executor executor, Handler handler, Retrofit oauthRetrofit, String accessToken,
String subredditName, String title, String crosspostFullname,
Flair flair, boolean isSpoiler, boolean isNSFW,

View File

@ -28,7 +28,6 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@ -55,7 +54,6 @@ import eu.toldi.infinityforlemmy.events.SubmitGalleryPostEvent;
import eu.toldi.infinityforlemmy.events.SubmitImagePostEvent;
import eu.toldi.infinityforlemmy.events.SubmitPollPostEvent;
import eu.toldi.infinityforlemmy.events.SubmitTextOrLinkPostEvent;
import eu.toldi.infinityforlemmy.events.SubmitVideoOrGifPostEvent;
import eu.toldi.infinityforlemmy.post.Post;
import eu.toldi.infinityforlemmy.post.SubmitPost;
import eu.toldi.infinityforlemmy.utils.APIUtils;
@ -157,11 +155,7 @@ public class SubmitPostService extends Service {
flair, isSpoiler, isNSFW, receivePostReplyNotifications);
} else if (postType == EXTRA_POST_TYPE_IMAGE) {
Uri mediaUri = Uri.parse(bundle.getString(EXTRA_MEDIA_URI));
submitImagePost(newAuthenticatorOauthRetrofit, account, mediaUri, subredditName, title, flair, isSpoiler, isNSFW,
receivePostReplyNotifications);
} else if (postType == EXTRA_POST_TYPE_VIDEO) {
Uri mediaUri = Uri.parse(bundle.getString(EXTRA_MEDIA_URI));
submitVideoPost(newAuthenticatorOauthRetrofit, account, mediaUri, subredditName, title, flair, isSpoiler, isNSFW,
submitImagePost(mRetrofit, account, mediaUri, subredditName, title, flair, isSpoiler, isNSFW,
receivePostReplyNotifications);
} else if (postType == EXTRA_POST_TYPE_GALLERY) {
submitGalleryPost(newAuthenticatorOauthRetrofit, account, bundle.getString(EXTRA_REDDIT_GALLERY_PAYLOAD));
@ -278,11 +272,11 @@ public class SubmitPostService extends Service {
});
}
private void submitImagePost(Retrofit newAuthenticatorOauthRetrofit, Account selectedAccount, Uri mediaUri, String subredditName, String title,
private void submitImagePost(RetrofitHolder newAuthenticatorOauthRetrofit, Account selectedAccount, Uri mediaUri, String subredditName, String title,
Flair flair, boolean isSpoiler, boolean isNSFW, boolean receivePostReplyNotifications) {
try {
Bitmap resource = Glide.with(this).asBitmap().load(mediaUri).submit().get();
SubmitPost.submitImagePost(mExecutor, handler, newAuthenticatorOauthRetrofit, mUploadMediaRetrofit,
SubmitPost.submitImagePost(mExecutor, handler, newAuthenticatorOauthRetrofit,
selectedAccount.getAccessToken(), subredditName, title, resource, flair, isSpoiler, isNSFW, receivePostReplyNotifications,
new SubmitPost.SubmitPostListener() {
@Override
@ -309,61 +303,6 @@ public class SubmitPostService extends Service {
}
}
private void submitVideoPost(Retrofit newAuthenticatorOauthRetrofit, Account selectedAccount, Uri mediaUri, String subredditName, String title,
Flair flair, boolean isSpoiler, boolean isNSFW, boolean receivePostReplyNotifications) {
try {
InputStream in = getContentResolver().openInputStream(mediaUri);
String type = getContentResolver().getType(mediaUri);
String cacheFilePath;
if (type != null && type.contains("gif")) {
cacheFilePath = getExternalCacheDir() + "/" + mediaUri.getLastPathSegment() + ".gif";
} else {
cacheFilePath = getExternalCacheDir() + "/" + mediaUri.getLastPathSegment() + ".mp4";
}
copyFileToCache(in, cacheFilePath);
Bitmap resource = Glide.with(this).asBitmap().load(mediaUri).submit().get();
if (type != null) {
SubmitPost.submitVideoPost(mExecutor, handler, newAuthenticatorOauthRetrofit, mUploadMediaRetrofit,
mUploadVideoRetrofit, selectedAccount.getAccessToken(), subredditName, title, new File(cacheFilePath),
type, resource, flair, isSpoiler, isNSFW, receivePostReplyNotifications,
new SubmitPost.SubmitPostListener() {
@Override
public void submitSuccessful(Post post) {
handler.post(() -> {
EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(true, false, null));
if (type.contains("gif")) {
Toast.makeText(SubmitPostService.this, R.string.gif_is_processing, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SubmitPostService.this, R.string.video_is_processing, Toast.LENGTH_SHORT).show();
}
});
stopService();
}
@Override
public void submitFailed(@Nullable String errorMessage) {
handler.post(() -> EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(false, false, errorMessage)));
stopService();
}
});
} else {
handler.post(() -> EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(false, true, null)));
stopService();
}
} catch (IOException | InterruptedException | ExecutionException e) {
e.printStackTrace();
handler.post(() -> EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(false, true, null)));
stopService();
}
}
private void submitGalleryPost(Retrofit newAuthenticatorOauthRetrofit, Account selectedAccount, String payload) {
try {

View File

@ -17,55 +17,41 @@ import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import eu.toldi.infinityforlemmy.apis.RedditAPI;
import eu.toldi.infinityforlemmy.RetrofitHolder;
import eu.toldi.infinityforlemmy.apis.LemmyAPI;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
public class UploadImageUtils {
@Nullable
public static String uploadImage(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
public static String uploadImage(RetrofitHolder mRetrofit,
String accessToken, Bitmap image) throws IOException, JSONException, XmlPullParserException {
return uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, image, false);
return uploadImage(mRetrofit, accessToken, image, false);
}
@Nullable
public static String uploadImage(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
String accessToken, Bitmap image, boolean returnResponseForGallerySubmission) throws IOException, JSONException, XmlPullParserException {
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
public static String uploadImage(RetrofitHolder mRetrofit,
String accessToken, Bitmap image, boolean returnResponseForGallerySubmission) throws IOException, JSONException, XmlPullParserException {
Map<String, String> uploadImageParams = new HashMap<>();
uploadImageParams.put(APIUtils.FILEPATH_KEY, "post_image.jpg");
uploadImageParams.put(APIUtils.MIMETYPE_KEY, "image/jpeg");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
Call<String> uploadImageCall = api.uploadImage(APIUtils.getOAuthHeader(accessToken), uploadImageParams);
Response<String> uploadImageResponse = uploadImageCall.execute();
if (uploadImageResponse.isSuccessful()) {
Map<String, RequestBody> nameValuePairsMap = parseJSONResponseFromAWS(uploadImageResponse.body());
RequestBody fileBody = RequestBody.create(byteArray, MediaType.parse("application/octet-stream"));
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("images[]", "post_image.jpg", fileBody);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
RequestBody fileBody = RequestBody.create(byteArray, MediaType.parse("application/octet-stream"));
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", "post_image.jpg", fileBody);
RedditAPI uploadMediaToAWSApi = uploadMediaRetrofit.create(RedditAPI.class);
Call<String> uploadMediaToAWS = uploadMediaToAWSApi.uploadMediaToAWS(nameValuePairsMap, fileToUpload);
Response<String> uploadMediaToAWSResponse = uploadMediaToAWS.execute();
if (uploadMediaToAWSResponse.isSuccessful()) {
if (returnResponseForGallerySubmission) {
return uploadImageResponse.body();
}
return parseXMLResponseFromAWS(uploadMediaToAWSResponse.body());
} else {
return "Error: " + uploadMediaToAWSResponse.code();
}
LemmyAPI api = mRetrofit.getRetrofit().create(LemmyAPI.class);
Call<String> uploadMedia = api.uploadImage("jwt=" + accessToken, fileToUpload);
Response<String> uploadMediaResponse = uploadMedia.execute();
if (uploadMediaResponse.isSuccessful()) {
JSONObject responseObject = new JSONObject(uploadMediaResponse.body());
String fileName = responseObject.getJSONArray("files").getJSONObject(0).getString("file");
return mRetrofit.getBaseURL() + "/pictrs/image/" + fileName;
} else {
return "Error: " + uploadImageResponse.message();
return "Error: " + uploadMediaResponse.code();
}
}

View File

@ -53,11 +53,11 @@ import java.util.concurrent.Executor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import io.noties.markwon.core.spans.CustomTypefaceSpan;
import eu.toldi.infinityforlemmy.R;
import eu.toldi.infinityforlemmy.RetrofitHolder;
import eu.toldi.infinityforlemmy.SortType;
import eu.toldi.infinityforlemmy.UploadedImage;
import retrofit2.Retrofit;
import io.noties.markwon.core.spans.CustomTypefaceSpan;
public final class Utils {
public static final int NETWORK_TYPE_OTHER = -1;
@ -361,8 +361,8 @@ public final class Utils {
return drawable;
}
public static void uploadImageToReddit(Context context, Executor executor, Retrofit oauthRetrofit,
Retrofit uploadMediaRetrofit, String accessToken, EditText editText,
public static void uploadImageToReddit(Context context, Executor executor, RetrofitHolder retrofit,
String accessToken, EditText editText,
CoordinatorLayout coordinatorLayout, Uri imageUri,
ArrayList<UploadedImage> uploadedImages) {
Toast.makeText(context, R.string.uploading_image, Toast.LENGTH_SHORT).show();
@ -370,7 +370,7 @@ public final class Utils {
executor.execute(() -> {
try {
Bitmap bitmap = Glide.with(context).asBitmap().load(imageUri).submit().get();
String imageUrlOrError = UploadImageUtils.uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, bitmap);
String imageUrlOrError = UploadImageUtils.uploadImage(retrofit, accessToken, bitmap);
handler.post(() -> {
if (imageUrlOrError != null && !imageUrlOrError.startsWith("Error: ")) {
String fileName = Utils.getFileName(context, imageUri);