Back end upload
This commit is contained in:
parent
de98ca8d79
commit
05e3f5b001
|
@ -26,6 +26,7 @@
|
|||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-feature android:name="android.hardware.camera" android:required="true" />
|
||||
<application
|
||||
android:name=".activities.MainApplication"
|
||||
|
|
|
@ -242,20 +242,23 @@ public class PeertubeEditUploadActivity extends BaseActivity implements OnRetrie
|
|||
@Override
|
||||
public void onRetrievePeertube(APIResponse apiResponse) {
|
||||
if( apiResponse.getError() != null || apiResponse.getPeertubes() == null || apiResponse.getPeertubes().size() == 0){
|
||||
if ( apiResponse.getError().getError() != null)
|
||||
if ( apiResponse.getError() != null && apiResponse.getError().getError() != null)
|
||||
Toasty.error(PeertubeEditUploadActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
|
||||
else
|
||||
Toasty.error(PeertubeEditUploadActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
|
||||
set_upload_submit.setEnabled(true);
|
||||
return;
|
||||
}
|
||||
|
||||
//Peertube video
|
||||
Peertube peertube = apiResponse.getPeertubes().get(0);
|
||||
new RetrievePeertubeChannelsAsyncTask(PeertubeEditUploadActivity.this, PeertubeEditUploadActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
|
||||
if( peertube.isUpdate()){
|
||||
Toasty.success(PeertubeEditUploadActivity.this, getString(R.string.toast_peertube_video_updated), Toast.LENGTH_LONG).show();
|
||||
peertube.setUpdate(false);
|
||||
set_upload_submit.setEnabled(true);
|
||||
}else {
|
||||
new RetrievePeertubeChannelsAsyncTask(PeertubeEditUploadActivity.this, PeertubeEditUploadActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
languageToSend = peertube.getLanguage();
|
||||
|
@ -263,15 +266,30 @@ public class PeertubeEditUploadActivity extends BaseActivity implements OnRetrie
|
|||
privacyToSend = peertube.getPrivacy();
|
||||
categoryToSend = peertube.getCategory();
|
||||
|
||||
String language = null;
|
||||
|
||||
Map.Entry<String,String> entryString = languageToSend.entrySet().iterator().next();
|
||||
String language = entryString.getValue();
|
||||
Map.Entry<Integer,String> entryInt = licenseToSend.entrySet().iterator().next();
|
||||
String license = entryInt.getValue();
|
||||
entryInt = privacyToSend.entrySet().iterator().next();
|
||||
String privacy = entryInt.getValue();
|
||||
entryInt = categoryToSend.entrySet().iterator().next();
|
||||
String category = entryInt.getValue();
|
||||
if( languageToSend != null) {
|
||||
Map.Entry<String, String> entryString = languageToSend.entrySet().iterator().next();
|
||||
language = entryString.getValue();
|
||||
}
|
||||
|
||||
String license = null;
|
||||
if( licenseToSend != null) {
|
||||
Map.Entry<Integer, String> entryInt = licenseToSend.entrySet().iterator().next();
|
||||
license = entryInt.getValue();
|
||||
}
|
||||
|
||||
String privacy = null;
|
||||
if( privacyToSend != null) {
|
||||
Map.Entry<Integer, String> entryInt = privacyToSend.entrySet().iterator().next();
|
||||
privacy = entryInt.getValue();
|
||||
}
|
||||
|
||||
String category = null;
|
||||
if( categoryToSend != null) {
|
||||
Map.Entry<Integer, String> entryInt = categoryToSend.entrySet().iterator().next();
|
||||
category = entryInt.getValue();
|
||||
}
|
||||
|
||||
channel = peertube.getChannel();
|
||||
String title = peertube.getName();
|
||||
|
@ -306,7 +324,7 @@ public class PeertubeEditUploadActivity extends BaseActivity implements OnRetrie
|
|||
}
|
||||
}
|
||||
int privacyPosition = 0;
|
||||
if( privacies.containsValue(privacy)){
|
||||
if( privacy != null && privacies.containsValue(privacy)){
|
||||
Iterator it = privacies.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry)it.next();
|
||||
|
@ -317,7 +335,7 @@ public class PeertubeEditUploadActivity extends BaseActivity implements OnRetrie
|
|||
}
|
||||
}
|
||||
int licensePosition = 0;
|
||||
if( licences.containsValue(license)){
|
||||
if( license != null && licences.containsValue(license)){
|
||||
Iterator it = licences.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry)it.next();
|
||||
|
@ -328,7 +346,7 @@ public class PeertubeEditUploadActivity extends BaseActivity implements OnRetrie
|
|||
}
|
||||
}
|
||||
int categoryPosition = 0;
|
||||
if( categories.containsValue(category)){
|
||||
if(category != null && categories.containsValue(category)){
|
||||
Iterator it = categories.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry)it.next();
|
||||
|
@ -429,6 +447,7 @@ public class PeertubeEditUploadActivity extends BaseActivity implements OnRetrie
|
|||
if( i == position){
|
||||
channelToSend = new HashMap<>();
|
||||
channelToSend.put((String)pair.getKey(), (String)pair.getValue());
|
||||
|
||||
break;
|
||||
}
|
||||
it.remove();
|
||||
|
@ -445,7 +464,6 @@ public class PeertubeEditUploadActivity extends BaseActivity implements OnRetrie
|
|||
String description = p_video_description.getText().toString().trim();
|
||||
boolean isNSFW = set_upload_nsfw.isChecked();
|
||||
boolean commentEnabled = set_upload_enable_comments.isChecked();
|
||||
Peertube peertubeSent = new Peertube();
|
||||
peertube.setName(title);
|
||||
peertube.setDescription(description);
|
||||
peertube.setSensitive(isNSFW);
|
||||
|
@ -454,9 +472,9 @@ public class PeertubeEditUploadActivity extends BaseActivity implements OnRetrie
|
|||
peertube.setLicense(licenseToSend);
|
||||
peertube.setLanguage(languageToSend);
|
||||
peertube.setChannelForUpdate(channelToSend);
|
||||
peertube.setPrivacy(privacyToSend);
|
||||
List<String> tags = p_video_tags.getTags();
|
||||
if( tags != null && tags.size() > 0)
|
||||
peertube.setTags(tags);
|
||||
peertube.setTags(tags);
|
||||
set_upload_submit.setEnabled(false);
|
||||
new PostPeertubeAsyncTask(PeertubeEditUploadActivity.this, peertube, PeertubeEditUploadActivity.this).executeOnExecutor(THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
@ -509,8 +527,11 @@ public class PeertubeEditUploadActivity extends BaseActivity implements OnRetrie
|
|||
Iterator it = channelsIterator.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry)it.next();
|
||||
if(pair.getKey().equals(channel.getUsername()))
|
||||
if(pair.getKey().equals(channel.getUsername())) {
|
||||
channelToSend = new HashMap<>();
|
||||
channelToSend.put((String)pair.getKey(), (String)pair.getValue());
|
||||
break;
|
||||
}
|
||||
it.remove();
|
||||
channelPosition++;
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ package fr.gouv.etalab.mastodon.activities;
|
|||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
|
@ -43,9 +44,21 @@ import android.widget.Toast;
|
|||
|
||||
import com.jaredrummler.materialspinner.MaterialSpinner;
|
||||
|
||||
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 org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import es.dmoral.toasty.Toasty;
|
||||
import fr.gouv.etalab.mastodon.R;
|
||||
|
@ -55,6 +68,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Account;
|
|||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrievePeertubeInterface;
|
||||
|
||||
import static fr.gouv.etalab.mastodon.asynctasks.RetrievePeertubeInformationAsyncTask.peertubeInformation;
|
||||
import static fr.gouv.etalab.mastodon.helper.Helper.THEME_LIGHT;
|
||||
import static fr.gouv.etalab.mastodon.helper.Helper.changeMaterialSpinnerColor;
|
||||
|
||||
|
@ -67,6 +81,11 @@ public class PeertubeUploadActivity extends BaseActivity implements OnRetrievePe
|
|||
private TextView set_upload_file_name;
|
||||
private HashMap<String, String> channels;
|
||||
private final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 724;
|
||||
private Uri uri;
|
||||
private String filename;
|
||||
private HashMap<Integer, String> privacyToSend;
|
||||
private HashMap<String, String> channelToSend;
|
||||
private String videoID;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -123,6 +142,8 @@ public class PeertubeUploadActivity extends BaseActivity implements OnRetrievePe
|
|||
|
||||
new RetrievePeertubeChannelsAsyncTask(PeertubeUploadActivity.this, PeertubeUploadActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
channels = new HashMap<>();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -137,10 +158,10 @@ public class PeertubeUploadActivity extends BaseActivity implements OnRetrievePe
|
|||
}
|
||||
set_upload_submit.setEnabled(true);
|
||||
|
||||
Uri uri = data.getData();
|
||||
uri = data.getData();
|
||||
String uriString = uri.toString();
|
||||
File myFile = new File(uriString);
|
||||
String filename = null;
|
||||
filename = null;
|
||||
if (uriString.startsWith("content://")) {
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
|
@ -186,24 +207,67 @@ public class PeertubeUploadActivity extends BaseActivity implements OnRetrievePe
|
|||
//Populate channels
|
||||
List<Account> accounts = apiResponse.getAccounts();
|
||||
String[] channelName = new String[accounts.size()];
|
||||
String[] channelId= new String[accounts.size()];
|
||||
int i = 0;
|
||||
for(Account account: accounts){
|
||||
channels.put(account.getUsername(),account.getId());
|
||||
channelName[i] = account.getUsername();
|
||||
channelId[i] = account.getId();
|
||||
i++;
|
||||
}
|
||||
|
||||
channelToSend = new HashMap<>();
|
||||
channelToSend.put(channelName[0], channelId[0]);
|
||||
ArrayAdapter<String> adapterChannel = new ArrayAdapter<>(PeertubeUploadActivity.this,
|
||||
android.R.layout.simple_spinner_dropdown_item, channelName);
|
||||
set_upload_channel.setAdapter(adapterChannel);
|
||||
|
||||
//Populate privacy
|
||||
String[] privacyName = new String[3];
|
||||
privacyName[0] = getString(R.string.v_public);
|
||||
privacyName[1] = getString(R.string.v_unlisted);
|
||||
privacyName[2] = getString(R.string.v_private);
|
||||
ArrayAdapter<String> adapterPrivacy = new ArrayAdapter<>(PeertubeUploadActivity.this,
|
||||
android.R.layout.simple_spinner_dropdown_item, privacyName);
|
||||
set_upload_privacy.setAdapter(adapterPrivacy);
|
||||
LinkedHashMap<String, String> translations = null;
|
||||
if( peertubeInformation.getTranslations() != null)
|
||||
translations = new LinkedHashMap<>(peertubeInformation.getTranslations());
|
||||
|
||||
LinkedHashMap<Integer, String> privaciesInit = new LinkedHashMap<>(peertubeInformation.getPrivacies());
|
||||
Map.Entry<Integer,String> entryInt = privaciesInit.entrySet().iterator().next();
|
||||
privacyToSend = new HashMap<>();
|
||||
privacyToSend.put(entryInt.getKey(), entryInt.getValue());
|
||||
LinkedHashMap<Integer, String> privacies = new LinkedHashMap<>(peertubeInformation.getPrivacies());
|
||||
//Populate privacies
|
||||
String[] privaciesA = new String[privacies.size()];
|
||||
Iterator it = privacies.entrySet().iterator();
|
||||
i = 0;
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry)it.next();
|
||||
if( translations == null || translations.size() == 0 || !translations.containsKey((String)pair.getValue()))
|
||||
privaciesA[i] = (String)pair.getValue();
|
||||
else
|
||||
privaciesA[i] = translations.get((String)pair.getValue());
|
||||
it.remove();
|
||||
i++;
|
||||
}
|
||||
|
||||
ArrayAdapter<String> adapterPrivacies = new ArrayAdapter<>(PeertubeUploadActivity.this,
|
||||
android.R.layout.simple_spinner_dropdown_item, privaciesA);
|
||||
set_upload_privacy.setAdapter(adapterPrivacies);
|
||||
|
||||
//Manage privacies
|
||||
set_upload_privacy.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
|
||||
@Override
|
||||
public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
|
||||
LinkedHashMap<Integer, String> privaciesCheck = new LinkedHashMap<>(peertubeInformation.getPrivacies());
|
||||
Iterator it = privaciesCheck.entrySet().iterator();
|
||||
int i = 0;
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry)it.next();
|
||||
if( i == position){
|
||||
privacyToSend = new HashMap<>();
|
||||
privacyToSend.put((Integer)pair.getKey(), (String)pair.getValue());
|
||||
break;
|
||||
}
|
||||
it.remove();
|
||||
i++;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
set_upload_file.setEnabled(true);
|
||||
|
||||
|
@ -236,5 +300,91 @@ public class PeertubeUploadActivity extends BaseActivity implements OnRetrievePe
|
|||
|
||||
}
|
||||
});
|
||||
|
||||
//Manage languages
|
||||
set_upload_channel.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
|
||||
@Override
|
||||
public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
|
||||
LinkedHashMap<String, String> channelsCheck = new LinkedHashMap<>(channels);
|
||||
Iterator it = channelsCheck.entrySet().iterator();
|
||||
int i = 0;
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry)it.next();
|
||||
if( i == position){
|
||||
channelToSend = new HashMap<>();
|
||||
channelToSend.put((String)pair.getKey(), (String)pair.getValue());
|
||||
|
||||
break;
|
||||
}
|
||||
it.remove();
|
||||
i++;
|
||||
}
|
||||
}
|
||||
});
|
||||
set_upload_submit.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if( uri != null) {
|
||||
Map.Entry<String, String> channelM = channelToSend.entrySet().iterator().next();
|
||||
String idChannel = channelM.getValue();
|
||||
Map.Entry<Integer, String> privacyM = privacyToSend.entrySet().iterator().next();
|
||||
Integer idPrivacy = privacyM.getKey();
|
||||
|
||||
try {
|
||||
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
|
||||
String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
|
||||
UploadNotificationConfig uploadConfig = new UploadNotificationConfig();
|
||||
Intent in = new Intent(getApplicationContext(), PeertubeEditUploadActivity.class );
|
||||
in.putExtra("video_id", videoID);
|
||||
PendingIntent clickIntent = PendingIntent.getActivity(getApplicationContext(), 1, in, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
uploadConfig
|
||||
.setClickIntentForAllStatuses(clickIntent)
|
||||
.setClearOnActionForAllStatuses(true);
|
||||
|
||||
String uploadId =
|
||||
new MultipartUploadRequest(PeertubeUploadActivity.this, "https://" + Helper.getLiveInstance(PeertubeUploadActivity.this) + "/api/v1/videos/upload")
|
||||
.addFileToUpload(uri.toString(), "videofile")
|
||||
.addHeader("Authorization", "Bearer " + token)
|
||||
.setNotificationConfig(uploadConfig)
|
||||
.addParameter("name", filename)
|
||||
.addParameter("channelId", idChannel)
|
||||
.addParameter("privacy", String.valueOf(idPrivacy))
|
||||
.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) {
|
||||
// your code here
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
|
||||
try {
|
||||
JSONObject response = new JSONObject(serverResponse.getBodyAsString());
|
||||
videoID = response.getJSONObject("video").get("id").toString();
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(Context context, UploadInfo uploadInfo) {
|
||||
// your code here
|
||||
}
|
||||
})
|
||||
.startUpload();
|
||||
finish();
|
||||
} catch (Exception exc) {
|
||||
exc.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,7 +62,8 @@ public class RetrievePeertubeSingleAsyncTask extends AsyncTask<Void, Void, Void>
|
|||
apiResponse = peertubeAPI.getSinglePeertube(this.instanceName, videoId);
|
||||
if (apiResponse.getPeertubes() != null && apiResponse.getPeertubes().size() > 0) {
|
||||
String rate = new PeertubeAPI(this.contextReference.get()).getRating(videoId);
|
||||
apiResponse.getPeertubes().get(0).setMyRating(rate);
|
||||
if( rate != null)
|
||||
apiResponse.getPeertubes().get(0).setMyRating(rate);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
|
|
@ -162,7 +162,7 @@ public class PeertubeAPI {
|
|||
@SuppressWarnings("SameParameterValue")
|
||||
public APIResponse updateVideo(Peertube peertube) {
|
||||
|
||||
HashMap<String, String> params = new HashMap<>();
|
||||
LinkedHashMap<String, String> params = new LinkedHashMap<>();
|
||||
|
||||
//Category
|
||||
Map.Entry<Integer,String> categoryM = peertube.getCategory().entrySet().iterator().next();
|
||||
|
@ -182,7 +182,7 @@ public class PeertubeAPI {
|
|||
params.put("privacy", String.valueOf(idPrivacy));
|
||||
//Channel
|
||||
Map.Entry<String,String> channelsM = peertube.getChannelForUpdate().entrySet().iterator().next();
|
||||
String iDChannel = channelsM.getKey();
|
||||
String iDChannel = channelsM.getValue();
|
||||
params.put("channelId", iDChannel);
|
||||
|
||||
|
||||
|
@ -194,21 +194,26 @@ public class PeertubeAPI {
|
|||
if( peertube.getTags() != null && peertube.getTags().size() > 0){
|
||||
int i = 0;
|
||||
for(String tag: peertube.getTags()){
|
||||
params.put("tags["+i+"]", tag);
|
||||
i++;
|
||||
params.put("tags["+(i++)+"]", tag);
|
||||
}
|
||||
/* StringBuilder parameters = new StringBuilder();
|
||||
for(String tag: peertube.getTags())
|
||||
parameters.append("tags[]=").append(tag).append("&");
|
||||
if( parameters.length() > 0) {
|
||||
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(10));
|
||||
params.put("tags[]", parameters.toString());
|
||||
}*/
|
||||
}else {
|
||||
params.put("tags", "null");
|
||||
}
|
||||
params.put("waitTranscoding", "true");
|
||||
params.put("support", "null");
|
||||
|
||||
List<Peertube> peertubes = new ArrayList<>();
|
||||
try {
|
||||
|
||||
HttpsConnection httpsConnection = new HttpsConnection(context);
|
||||
String response = httpsConnection.put(getAbsoluteUrl(String.format("/videos/%s", peertube.getId())), 60, params, prefKeyOauthTokenT);
|
||||
|
||||
JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
|
||||
peertubes = parsePeertube(jsonArray);
|
||||
|
||||
httpsConnection.put(getAbsoluteUrl(String.format("/videos/%s", peertube.getId())), 60, params, prefKeyOauthTokenT);
|
||||
peertubes.add(peertube);
|
||||
} catch (HttpsConnection.HttpsConnectionException e) {
|
||||
setError(e.getStatusCode(), e);
|
||||
e.printStackTrace();
|
||||
|
@ -218,8 +223,6 @@ public class PeertubeAPI {
|
|||
e.printStackTrace();
|
||||
} catch (KeyManagementException e) {
|
||||
e.printStackTrace();
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
apiResponse.setPeertubes(peertubes);
|
||||
return apiResponse;
|
||||
|
|
|
@ -67,7 +67,7 @@
|
|||
android:minLines="2"
|
||||
android:lines="2"
|
||||
android:layout_height="wrap_content"
|
||||
TagsEditText:allowSpaceInTag="false"
|
||||
TagsEditText:allowSpaceInTag="true"
|
||||
TagsEditText:tagsCloseImageRight="@drawable/tag_close"
|
||||
TagsEditText:tagsTextColor="?colorAccent"
|
||||
TagsEditText:tagsTextSize="@dimen/defaultTagsTextSize"
|
||||
|
|
Loading…
Reference in New Issue