diff --git a/README.md b/README.md index 43cbd87..61e7b40 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ The other app is **[TubeLab](#TubeLab)** a Peertube Android app working for all ## TubeLab -Tubelab is an Android app for Peertube (GNU GPLv3). +Tubelab is an Android app for Peertube (GNU GPLv3). [Get it on Google Play](https://play.google.com/store/apps/details?id=app.fedilab.tubelab)   [Get it on F-Droid](https://f-droid.org/packages/app.fedilab.tubelab/) diff --git a/app/src/main/java/app/fedilab/fedilabtube/PeertubeEditUploadActivity.java b/app/src/main/java/app/fedilab/fedilabtube/PeertubeEditUploadActivity.java index 5656472..cfbf813 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/PeertubeEditUploadActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/PeertubeEditUploadActivity.java @@ -16,7 +16,6 @@ package app.fedilab.fedilabtube; import android.Manifest; import android.app.Activity; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; @@ -37,20 +36,14 @@ import androidx.core.content.ContextCompat; import androidx.lifecycle.ViewModelProvider; import com.bumptech.glide.Glide; +import com.bumptech.glide.load.resource.bitmap.CenterCrop; +import com.bumptech.glide.load.resource.bitmap.RoundedCorners; +import com.bumptech.glide.request.RequestOptions; -import net.gotev.uploadservice.MultipartUploadRequest; -import net.gotev.uploadservice.ServerResponse; -import net.gotev.uploadservice.UploadInfo; -import net.gotev.uploadservice.UploadNotificationConfig; -import net.gotev.uploadservice.UploadStatusDelegate; - -import java.io.FileNotFoundException; -import java.net.MalformedURLException; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.UUID; import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI; @@ -87,6 +80,7 @@ public class PeertubeEditUploadActivity extends AppCompatActivity { private Video video; private String channelToSendId; private ActivityPeertubeEditBinding binding; + private Uri inputData; @Override protected void onCreate(Bundle savedInstanceState) { @@ -458,14 +452,13 @@ public class PeertubeEditUploadActivity extends AppCompatActivity { videoParams.setCategory(finalCategoryToSend.getId()); videoParams.setLicence(String.valueOf(finalLicenseToSend.getId())); videoParams.setLanguage(finalLanguageToSend.getId()); - videoParams.setChannelId(channelToSendId); videoParams.setPrivacy(finalPrivacyToSend.getId()); List tags = binding.pVideoTags.getTags(); videoParams.setTags(tags); binding.setUploadSubmit.setEnabled(false); MyVideoVM myVideoVM = new ViewModelProvider(PeertubeEditUploadActivity.this).get(MyVideoVM.class); - myVideoVM.updateVideo(videoId, videoParams, null, null).observe(PeertubeEditUploadActivity.this, this::manageUpdate); + myVideoVM.updateVideo(videoId, videoParams, inputData, inputData).observe(PeertubeEditUploadActivity.this, this::manageUpdate); }); binding.setUploadPrivacy.setSelection(privacyPosition, false); @@ -570,53 +563,12 @@ public class PeertubeEditUploadActivity extends AppCompatActivity { Toasty.error(PeertubeEditUploadActivity.this, getString(R.string.toot_select_image_error), Toast.LENGTH_LONG).show(); return; } - UploadNotificationConfig uploadConfig = new UploadNotificationConfig(); - uploadConfig.getCompleted().autoClear = true; - try { - SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); - Uri uri = data.getData(); - try { - String uploadId = UUID.randomUUID().toString(); - new MultipartUploadRequest(PeertubeEditUploadActivity.this, uploadId, "https://" + Helper.getLiveInstance(PeertubeEditUploadActivity.this) + "/api/v1/" + String.format("/videos/%s", video.getId())) - .addFileToUpload(uri.toString().replace("file://", ""), "thumbnailfile") - .setMethod("PUT") - .addHeader("Authorization", "Bearer " + token) - .setNotificationConfig(uploadConfig) - .setMaxRetries(2) - .setDelegate(new UploadStatusDelegate() { - @Override - public void onProgress(Context context, UploadInfo uploadInfo) { - // your code here - } - - @Override - public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, - Exception exception) { - Toasty.error(PeertubeEditUploadActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show(); - } - - @Override - public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) { - Glide.with(PeertubeEditUploadActivity.this) - .load(uri) - .into(binding.pVideoPreview); - } - - @Override - public void onCancelled(Context context, UploadInfo uploadInfo) { - // your code here - } - }) - .startUpload(); - } catch (FileNotFoundException e) { - Toasty.error(PeertubeEditUploadActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show(); - e.printStackTrace(); - } - } catch (MalformedURLException e) { - Toasty.error(PeertubeEditUploadActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show(); - e.printStackTrace(); - } + inputData = data.getData(); + Glide.with(PeertubeEditUploadActivity.this) + .load(data.getData()) + .thumbnail(0.1f) + .apply(new RequestOptions().transform(new CenterCrop(), new RoundedCorners(10))) + .into(binding.pVideoPreview); } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java index 1e4f41b..4c1e25d 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java @@ -19,10 +19,15 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; +import android.net.Uri; import android.os.Handler; import android.os.Looper; import android.webkit.MimeTypeMap; +import androidx.documentfile.provider.DocumentFile; + +import org.jetbrains.annotations.NotNull; + import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; @@ -559,22 +564,7 @@ public class RetrofitPeertubeAPI { throw error; } if (userSettings.getAvatarfile() != null) { - InputStream inputStream = _context.getContentResolver().openInputStream(userSettings.getAvatarfile()); - ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); - int bufferSize = 1024; - byte[] buffer = new byte[bufferSize]; - - int len; - while ((len = inputStream.read(buffer)) != -1) { - byteBuffer.write(buffer, 0, len); - } - byte[] imageBytes = byteBuffer.toByteArray(); - String mime = MimeTypeMap.getFileExtensionFromUrl(userSettings.getAvatarfile().toString()); - if (mime == null || mime.trim().length() == 0) { - mime = "png"; - } - RequestBody requestFile = RequestBody.create(MediaType.parse("image/" + mime), imageBytes); - MultipartBody.Part bodyThumbnail = MultipartBody.Part.createFormData("avatarfile", userSettings.getFileName(), requestFile); + MultipartBody.Part bodyThumbnail = createFile("avatarfile", userSettings.getAvatarfile(), userSettings.getFileName()); Call updateProfilePicture = peertubeService.updateProfilePicture(getToken(), bodyThumbnail); Response responseAvatar = updateProfilePicture.execute(); if (response.isSuccessful()) { @@ -594,6 +584,29 @@ public class RetrofitPeertubeAPI { return avatarResponse; } + private MultipartBody.Part createFile(@NotNull String paramName, @NotNull Uri uri, String filename) throws IOException { + + InputStream inputStream = _context.getContentResolver().openInputStream(uri); + ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); + int bufferSize = 1024; + byte[] buffer = new byte[bufferSize]; + + int len; + while ((len = inputStream.read(buffer)) != -1) { + byteBuffer.write(buffer, 0, len); + } + byte[] imageBytes = byteBuffer.toByteArray(); + String mime = MimeTypeMap.getFileExtensionFromUrl(uri.toString()); + if (mime == null || mime.trim().length() == 0) { + mime = "png"; + } + if (filename == null) { + filename = "my_image." + mime; + } + RequestBody requestFile = RequestBody.create(MediaType.parse("image/" + mime), imageBytes); + return MultipartBody.Part.createFormData(paramName, filename, requestFile); + } + /** * Check if users via their uris are following the authenticated user * @@ -949,17 +962,30 @@ public class RetrofitPeertubeAPI { * @param previewfile File preview * @return APIResponse */ - public APIResponse updateVideo(String videoId, VideoParams videoParams, File thumbnail, File previewfile) { + public APIResponse updateVideo(String videoId, VideoParams videoParams, Uri thumbnail, Uri previewfile) { PeertubeService peertubeService = init(); + MultipartBody.Part bodyThumbnail = null; MultipartBody.Part bodyPreviewfile = null; - if (thumbnail != null) { - RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), thumbnail); - bodyThumbnail = MultipartBody.Part.createFormData("image", thumbnail.getName(), requestFile); - } - if (previewfile != null) { - RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), previewfile); - bodyPreviewfile = MultipartBody.Part.createFormData("image", previewfile.getName(), requestFile); + try { + if (thumbnail != null) { + DocumentFile documentFile = DocumentFile.fromSingleUri(_context, thumbnail); + String thumbnailName = null; + if (documentFile != null) { + thumbnailName = documentFile.getName(); + } + bodyThumbnail = createFile("avatarfile", thumbnail, thumbnailName); + } + if (previewfile != null) { + DocumentFile documentFile = DocumentFile.fromSingleUri(_context, thumbnail); + String previewfileName = null; + if (documentFile != null) { + previewfileName = documentFile.getName(); + } + bodyPreviewfile = createFile("image", previewfile, previewfileName); + } + } catch (IOException e) { + e.printStackTrace(); } RequestBody channelId = RequestBody.create(MediaType.parse("text/plain"), videoParams.getChannelId()); RequestBody description = RequestBody.create(MediaType.parse("text/plain"), videoParams.getDescription()); diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/MyVideoVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/MyVideoVM.java index 55653a2..0052a69 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/MyVideoVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/MyVideoVM.java @@ -16,6 +16,7 @@ package app.fedilab.fedilabtube.viewmodel; import android.app.Application; import android.content.Context; +import android.net.Uri; import android.os.Handler; import android.os.Looper; @@ -24,8 +25,6 @@ import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; -import java.io.File; - import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI; import app.fedilab.fedilabtube.client.entities.VideoParams; @@ -39,13 +38,13 @@ public class MyVideoVM extends AndroidViewModel { } - public LiveData updateVideo(String videoId, VideoParams videoParams, File thumbnail, File previewfile) { + public LiveData updateVideo(String videoId, VideoParams videoParams, Uri thumbnail, Uri previewfile) { apiResponseMutableLiveData = new MutableLiveData<>(); update(videoId, videoParams, thumbnail, previewfile); return apiResponseMutableLiveData; } - private void update(String videoId, VideoParams videoParams, File thumbnail, File previewfile) { + private void update(String videoId, VideoParams videoParams, Uri thumbnail, Uri previewfile) { Context _mContext = getApplication().getApplicationContext(); new Thread(() -> { try {