Change request

This commit is contained in:
Thomas 2020-09-04 15:56:18 +02:00
parent 1334419ed9
commit 9eacccf504
3 changed files with 155 additions and 126 deletions

View File

@ -15,9 +15,10 @@ package app.fedilab.fedilabtube;
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.InputFilter; import android.text.InputFilter;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
@ -38,13 +39,6 @@ import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
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.net.MalformedURLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -55,10 +49,12 @@ import java.util.Map;
import app.fedilab.fedilabtube.asynctasks.ManagePlaylistsAsyncTask; import app.fedilab.fedilabtube.asynctasks.ManagePlaylistsAsyncTask;
import app.fedilab.fedilabtube.asynctasks.RetrievePeertubeChannelsAsyncTask; import app.fedilab.fedilabtube.asynctasks.RetrievePeertubeChannelsAsyncTask;
import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.APIResponse;
import app.fedilab.fedilabtube.client.HttpsConnection;
import app.fedilab.fedilabtube.client.PeertubeAPI;
import app.fedilab.fedilabtube.client.entities.Account; import app.fedilab.fedilabtube.client.entities.Account;
import app.fedilab.fedilabtube.client.entities.Playlist; import app.fedilab.fedilabtube.client.entities.Playlist;
import app.fedilab.fedilabtube.client.entities.PlaylistElement;
import app.fedilab.fedilabtube.drawer.PlaylistAdapter; import app.fedilab.fedilabtube.drawer.PlaylistAdapter;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.interfaces.OnPlaylistActionInterface; import app.fedilab.fedilabtube.interfaces.OnPlaylistActionInterface;
import app.fedilab.fedilabtube.interfaces.OnRetrievePeertubeInterface; import app.fedilab.fedilabtube.interfaces.OnRetrievePeertubeInterface;
import es.dmoral.toasty.Toasty; import es.dmoral.toasty.Toasty;
@ -72,15 +68,13 @@ public class AllPlaylistsActivity extends AppCompatActivity implements OnPlaylis
private AsyncTask<Void, Void, Void> asyncTask; private AsyncTask<Void, Void, Void> asyncTask;
private RelativeLayout mainLoader; private RelativeLayout mainLoader;
private FloatingActionButton add_new;
private RelativeLayout textviewNoAction; private RelativeLayout textviewNoAction;
private HashMap<Integer, String> privacyToSend; private HashMap<Integer, String> privacyToSend;
private HashMap<String, String> channelToSend;
private Spinner set_upload_channel; private Spinner set_upload_channel;
private Spinner set_upload_privacy; private Spinner set_upload_privacy;
private HashMap<String, String> channels; private String idChannel;
private List<Playlist> playlists;
PlaylistAdapter playlistAdapter;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -97,10 +91,10 @@ public class AllPlaylistsActivity extends AppCompatActivity implements OnPlaylis
RelativeLayout nextElementLoader = findViewById(R.id.loading_next_items); RelativeLayout nextElementLoader = findViewById(R.id.loading_next_items);
mainLoader.setVisibility(View.VISIBLE); mainLoader.setVisibility(View.VISIBLE);
nextElementLoader.setVisibility(View.GONE); nextElementLoader.setVisibility(View.GONE);
idChannel = null;
asyncTask = new ManagePlaylistsAsyncTask(AllPlaylistsActivity.this, ManagePlaylistsAsyncTask.action.GET_PLAYLIST, null, null, null, AllPlaylistsActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); asyncTask = new ManagePlaylistsAsyncTask(AllPlaylistsActivity.this, ManagePlaylistsAsyncTask.action.GET_PLAYLIST, null, null, null, AllPlaylistsActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
add_new = findViewById(R.id.add_new); FloatingActionButton add_new = findViewById(R.id.add_new);
LinkedHashMap<Integer, String> privaciesInit = new LinkedHashMap<>(peertubeInformation.getPrivacies()); LinkedHashMap<Integer, String> privaciesInit = new LinkedHashMap<>(peertubeInformation.getPrivacies());
@ -111,8 +105,12 @@ public class AllPlaylistsActivity extends AppCompatActivity implements OnPlaylis
} }
playlists = new ArrayList<>();
ListView lv_playlist = findViewById(R.id.lv_playlist);
playlistAdapter = new PlaylistAdapter(AllPlaylistsActivity.this, playlists, textviewNoAction);
lv_playlist.setAdapter(playlistAdapter);
add_new.setOnClickListener(view -> { add_new.setOnClickListener(view -> {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(AllPlaylistsActivity.this); AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(AllPlaylistsActivity.this);
LayoutInflater inflater1 = getLayoutInflater(); LayoutInflater inflater1 = getLayoutInflater();
View dialogView = inflater1.inflate(R.layout.add_playlist, new LinearLayout(AllPlaylistsActivity.this), false); View dialogView = inflater1.inflate(R.layout.add_playlist, new LinearLayout(AllPlaylistsActivity.this), false);
@ -131,72 +129,51 @@ public class AllPlaylistsActivity extends AppCompatActivity implements OnPlaylis
dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> { dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> {
if (display_name.getText() != null && display_name.getText().toString().trim().length() > 0) { if (display_name.getText() != null && display_name.getText().toString().trim().length() > 0) {
PlaylistElement playlistElement = new PlaylistElement();
Playlist playlist = new Playlist(); playlistElement.setDisplayName(display_name.getText().toString().trim());
playlist.setDisplayName(display_name.getText().toString().trim());
if (description.getText() != null && description.getText().toString().trim().length() > 0) { if (description.getText() != null && description.getText().toString().trim().length() > 0) {
playlist.setDescription(description.getText().toString().trim()); playlistElement.setDescription(description.getText().toString().trim());
}
String idChannel = null;
if (channelToSend != null) {
Map.Entry<String, String> channelM = channelToSend.entrySet().iterator().next();
idChannel = channelM.getValue();
if (idChannel.length() > 0)
playlist.setVideoChannelId(idChannel);
} }
playlistElement.setVideoChannelId(idChannel);
String idPrivacy;
String label;
Map.Entry<Integer, String> privacyM = privacyToSend.entrySet().iterator().next(); Map.Entry<Integer, String> privacyM = privacyToSend.entrySet().iterator().next();
String label = privacyM.getValue(); idPrivacy = String.valueOf(privacyM.getKey());
String idPrivacy = String.valueOf(privacyM.getKey()); label = privacyM.getValue();
if (label.equals("Public") && (playlist.getVideoChannelId() == null || playlist.getVideoChannelId().equals(""))) { if ((label.equals("Public") && (playlistElement.getVideoChannelId() == null || playlistElement.getVideoChannelId().equals("")))) {
Toasty.error(AllPlaylistsActivity.this, getString(R.string.error_channel_mandatory), Toast.LENGTH_LONG).show(); Toasty.error(AllPlaylistsActivity.this, getString(R.string.error_channel_mandatory), Toast.LENGTH_LONG).show();
} else { } else {
if (privacyToSend != null) { if (privacyToSend != null) {
playlist.setPrivacy(privacyToSend); playlistElement.setPrivacy(idPrivacy);
} }
//new ManagePlaylistsAsyncTask(context, ManagePlaylistsAsyncTask.action.CREATE_PLAYLIST, playlist, null, null, DisplayPlaylistsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); new Thread(() -> {
UploadNotificationConfig uploadConfig = new UploadNotificationConfig(); try {
uploadConfig.getCompleted().autoClear = true; String playlistId = new PeertubeAPI(AllPlaylistsActivity.this).createPlaylist(playlistElement);
try { Handler mainHandler = new Handler(Looper.getMainLooper());
String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); Runnable myRunnable = () -> {
new MultipartUploadRequest(AllPlaylistsActivity.this, "https://" + Helper.getLiveInstance(AllPlaylistsActivity.this) + "/api/v1/video-playlists/") Playlist playlist = new Playlist();
//.addFileToUpload(uri.toString().replace("file://",""), "videofile") playlist.setId(playlistId);
.addHeader("Authorization", "Bearer " + token) playlist.setDescription(playlistElement.getDescription());
.setNotificationConfig(uploadConfig) playlist.setDisplayName(playlistElement.getDisplayName());
// .addParameter("name", filename) playlist.setPrivacy(privacyToSend);
.addParameter("videoChannelId", idChannel) playlists.add(playlist);
.addParameter("privacy", idPrivacy) playlistAdapter.notifyDataSetChanged();
.addParameter("displayName", playlist.getDisplayName()) };
.addParameter("description", playlist.getDescription()) mainHandler.post(myRunnable);
.setMaxRetries(1) } catch (HttpsConnection.HttpsConnectionException e) {
.setDelegate(new UploadStatusDelegate() { e.printStackTrace();
@Override Handler mainHandler = new Handler(Looper.getMainLooper());
public void onProgress(Context context, UploadInfo uploadInfo) { Runnable myRunnable = () -> {
// your code here if (e.getMessage() != null) {
} Toasty.error(AllPlaylistsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
} else {
@Override Toasty.error(AllPlaylistsActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, }
Exception exception) { };
// your code here mainHandler.post(myRunnable);
} }
}).start();
@Override
public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
asyncTask = new ManagePlaylistsAsyncTask(AllPlaylistsActivity.this, ManagePlaylistsAsyncTask.action.GET_PLAYLIST, null, null, null, AllPlaylistsActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override
public void onCancelled(Context context, UploadInfo uploadInfo) {
// your code here
}
})
.startUpload();
} catch (MalformedURLException e) {
e.printStackTrace();
}
dialog.dismiss(); dialog.dismiss();
add_new.setEnabled(false);
} }
} else { } else {
Toasty.error(AllPlaylistsActivity.this, getString(R.string.error_display_name), Toast.LENGTH_LONG).show(); Toasty.error(AllPlaylistsActivity.this, getString(R.string.error_display_name), Toast.LENGTH_LONG).show();
@ -241,7 +218,6 @@ public class AllPlaylistsActivity extends AppCompatActivity implements OnPlaylis
@Override @Override
public void onActionDone(ManagePlaylistsAsyncTask.action actionType, APIResponse apiResponse, int statusCode) { public void onActionDone(ManagePlaylistsAsyncTask.action actionType, APIResponse apiResponse, int statusCode) {
mainLoader.setVisibility(View.GONE); mainLoader.setVisibility(View.GONE);
add_new.setEnabled(true);
if (apiResponse.getError() != null) { if (apiResponse.getError() != null) {
Toasty.error(AllPlaylistsActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show(); Toasty.error(AllPlaylistsActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
return; return;
@ -249,11 +225,8 @@ public class AllPlaylistsActivity extends AppCompatActivity implements OnPlaylis
if (actionType == ManagePlaylistsAsyncTask.action.GET_PLAYLIST) { if (actionType == ManagePlaylistsAsyncTask.action.GET_PLAYLIST) {
if (apiResponse.getPlaylists() != null && apiResponse.getPlaylists().size() > 0) { if (apiResponse.getPlaylists() != null && apiResponse.getPlaylists().size() > 0) {
List<Playlist> playlists = new ArrayList<>();
ListView lv_playlist = findViewById(R.id.lv_playlist);
PlaylistAdapter playlistAdapter = new PlaylistAdapter(AllPlaylistsActivity.this, playlists, textviewNoAction);
playlists.addAll(apiResponse.getPlaylists()); playlists.addAll(apiResponse.getPlaylists());
lv_playlist.setAdapter(playlistAdapter); playlistAdapter.notifyDataSetChanged();
textviewNoAction.setVisibility(View.GONE); textviewNoAction.setVisibility(View.GONE);
} else { } else {
textviewNoAction.setVisibility(View.VISIBLE); textviewNoAction.setVisibility(View.VISIBLE);
@ -288,17 +261,14 @@ public class AllPlaylistsActivity extends AppCompatActivity implements OnPlaylis
String[] channelId = new String[accounts.size() + 1]; String[] channelId = new String[accounts.size() + 1];
int i = 1; int i = 1;
channelName[0] = ""; channelName[0] = "";
channelId[0] = ""; channelId[0] = "null";
channels = new HashMap<>();
for (Account account : accounts) { for (Account account : accounts) {
channels.put(account.getUsername(), account.getId());
channelName[i] = account.getUsername(); channelName[i] = account.getUsername();
channelId[i] = account.getId(); channelId[i] = account.getId();
i++; i++;
} }
channelToSend = new HashMap<>();
channelToSend.put(channelName[0], channelId[0]);
ArrayAdapter<String> adapterChannel = new ArrayAdapter<>(AllPlaylistsActivity.this, ArrayAdapter<String> adapterChannel = new ArrayAdapter<>(AllPlaylistsActivity.this,
android.R.layout.simple_spinner_dropdown_item, channelName); android.R.layout.simple_spinner_dropdown_item, channelName);
set_upload_channel.setAdapter(adapterChannel); set_upload_channel.setAdapter(adapterChannel);
@ -358,19 +328,7 @@ public class AllPlaylistsActivity extends AppCompatActivity implements OnPlaylis
set_upload_channel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { set_upload_channel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
LinkedHashMap<String, String> channelsCheck = new LinkedHashMap<>(channels); idChannel = channelId[position];
Iterator<Map.Entry<String, String>> it = channelsCheck.entrySet().iterator();
int i = 0;
while (it.hasNext()) {
Map.Entry<String, String> pair = it.next();
if (i == position) {
channelToSend = new HashMap<>();
channelToSend.put(pair.getKey(), pair.getValue());
break;
}
it.remove();
i++;
}
} }
@Override @Override

View File

@ -61,6 +61,7 @@ import app.fedilab.fedilabtube.helper.FileNameCleaner;
import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.interfaces.OnDownloadInterface; import app.fedilab.fedilabtube.interfaces.OnDownloadInterface;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class HttpsConnection { public class HttpsConnection {
@ -158,10 +159,8 @@ public class HttpsConnection {
httpsURLConnection.setUseCaches(true); httpsURLConnection.setUseCaches(true);
httpsURLConnection.setDefaultUseCaches(true); httpsURLConnection.setDefaultUseCaches(true);
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory()); httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
if (token != null && !token.startsWith("Basic ")) if (token != null)
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
else if (token != null && token.startsWith("Basic "))
httpsURLConnection.setRequestProperty("Authorization", token);
httpsURLConnection.setRequestMethod("GET"); httpsURLConnection.setRequestMethod("GET");
String response; String response;
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) { if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
@ -243,6 +242,87 @@ public class HttpsConnection {
return response; return response;
} }
public String postBoundary(String urlConnection, int timeout, LinkedHashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
URL url = new URL(urlConnection);
String boundary = "----TubeLabBoundary" + System.currentTimeMillis();
if (proxy != null)
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
else
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setConnectTimeout(timeout * 1000);
httpsURLConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
if (token != null)
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
StringBuilder postData = new StringBuilder();
if (paramaters != null) {
Iterator<Map.Entry<String, String>> it = paramaters.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pair = it.next();
postData = addFormField(postData,pair.getKey(), pair.getValue(), boundary);
it.remove();
}
postData.append("--").append(boundary).append("--");
}
byte[] postDataBytes = postData.toString().getBytes(StandardCharsets.UTF_8);
httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
OutputStream outputStream = httpsURLConnection.getOutputStream();
outputStream.write(postDataBytes);
String response;
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
getSinceMaxId();
response = converToString(httpsURLConnection.getInputStream());
} else {
String error = null;
if (httpsURLConnection.getErrorStream() != null) {
InputStream stream = httpsURLConnection.getErrorStream();
if (stream == null) {
stream = httpsURLConnection.getInputStream();
}
try (Scanner scanner = new Scanner(stream)) {
scanner.useDelimiter("\\Z");
if (scanner.hasNext()) {
error = scanner.next();
}
} catch (Exception e) {
e.printStackTrace();
}
}
int responseCode = httpsURLConnection.getResponseCode();
try {
httpsURLConnection.getInputStream().close();
} catch (Exception ignored) {
}
throw new HttpsConnectionException(responseCode, error);
}
getSinceMaxId();
httpsURLConnection.getInputStream().close();
return response;
}
private static final String LINE_FEED = "\r\n";
/**
* Adds a form field to the request
*@param writer StringBuilder
* @param name field name
* @param value field value
*/
private StringBuilder addFormField(StringBuilder writer, String name, String value, String boundary) {
if( writer != null) {
writer.append("--").append(boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"").append(name).append("\"")
.append(LINE_FEED);
writer.append("Content-Type: text/plain; charset=").append(StandardCharsets.UTF_8).append(
LINE_FEED);
writer.append(LINE_FEED);
writer.append(value).append(LINE_FEED);
}
return writer;
}
public String post(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException { public String post(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
URL url = new URL(urlConnection); URL url = new URL(urlConnection);
@ -271,10 +351,8 @@ public class HttpsConnection {
httpsURLConnection.setDoOutput(true); httpsURLConnection.setDoOutput(true);
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory()); httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
httpsURLConnection.setRequestMethod("POST"); httpsURLConnection.setRequestMethod("POST");
if (token != null && !token.startsWith("Basic ")) if (token != null)
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
else if (token != null && token.startsWith("Basic "))
httpsURLConnection.setRequestProperty("Authorization", token);
httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
@ -495,10 +573,8 @@ public class HttpsConnection {
httpsURLConnection.setConnectTimeout(timeout * 1000); httpsURLConnection.setConnectTimeout(timeout * 1000);
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory()); httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
httpsURLConnection.setRequestMethod("PATCH"); httpsURLConnection.setRequestMethod("PATCH");
if (token != null && !token.startsWith("Basic ")) if (token != null)
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
else if (token != null && token.startsWith("Basic "))
httpsURLConnection.setRequestProperty("Authorization", token);
httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
httpsURLConnection.setDoOutput(true); httpsURLConnection.setDoOutput(true);
@ -543,7 +619,6 @@ public class HttpsConnection {
} }
httpsURLConnection.getInputStream().close(); httpsURLConnection.getInputStream().close();
return response; return response;
} }
public String put(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException { public String put(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
@ -573,10 +648,8 @@ public class HttpsConnection {
httpsURLConnection = (HttpsURLConnection) url.openConnection(); httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setConnectTimeout(timeout * 1000); httpsURLConnection.setConnectTimeout(timeout * 1000);
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory()); httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
if (token != null && !token.startsWith("Basic ")) if (token != null)
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
else if (token != null && token.startsWith("Basic "))
httpsURLConnection.setRequestProperty("Authorization", token);
httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
httpsURLConnection.setRequestMethod("PUT"); httpsURLConnection.setRequestMethod("PUT");
@ -643,10 +716,8 @@ public class HttpsConnection {
else else
httpsURLConnection = (HttpsURLConnection) url.openConnection(); httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory()); httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
if (token != null && !token.startsWith("Basic ")) if (token != null)
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token); httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
else if (token != null && token.startsWith("Basic "))
httpsURLConnection.setRequestProperty("Authorization", token);
httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpsURLConnection.setRequestMethod("DELETE"); httpsURLConnection.setRequestMethod("DELETE");
httpsURLConnection.setConnectTimeout(timeout * 1000); httpsURLConnection.setConnectTimeout(timeout * 1000);

View File

@ -874,9 +874,8 @@ public class PeertubeAPI {
* Delete a Channel * Delete a Channel
* *
* @param channelId String, the channel id * @param channelId String, the channel id
* @return int
*/ */
public int deleteChannel(String channelId) throws HttpsConnection.HttpsConnectionException { public void deleteChannel(String channelId) throws HttpsConnection.HttpsConnectionException {
try { try {
HttpsConnection httpsConnection = new HttpsConnection(context); HttpsConnection httpsConnection = new HttpsConnection(context);
httpsConnection.delete(getAbsoluteUrl(String.format("/video-channels/%s", channelId)), 60, null, prefKeyOauthTokenT); httpsConnection.delete(getAbsoluteUrl(String.format("/video-channels/%s", channelId)), 60, null, prefKeyOauthTokenT);
@ -884,7 +883,6 @@ public class PeertubeAPI {
} catch (NoSuchAlgorithmException | IOException | KeyManagementException e) { } catch (NoSuchAlgorithmException | IOException | KeyManagementException e) {
e.printStackTrace(); e.printStackTrace();
} }
return actionCode;
} }
@ -922,6 +920,7 @@ public class PeertubeAPI {
* @param accountId String account fetched * @param accountId String account fetched
* @return Account entity * @return Account entity
*/ */
@SuppressWarnings("unused")
public Account getAccount(String accountId) { public Account getAccount(String accountId) {
account = new Account(); account = new Account();
@ -1712,21 +1711,22 @@ public class PeertubeAPI {
* Create a Playlist * Create a Playlist
* *
* @param playlistElement PlaylistElement, the playlist elements * @param playlistElement PlaylistElement, the playlist elements
* @return int
*/ */
public int createPlaylist(PlaylistElement playlistElement) throws HttpsConnection.HttpsConnectionException { public String createPlaylist(PlaylistElement playlistElement) throws HttpsConnection.HttpsConnectionException {
String playlistId = "-1";
try { try {
HttpsConnection httpsConnection = new HttpsConnection(context); HttpsConnection httpsConnection = new HttpsConnection(context);
HashMap<String, String> params = new HashMap<>(); LinkedHashMap<String, String> params = new LinkedHashMap<>();
params.put("videoChannelId", playlistElement.getVideoChannelId());
params.put("privacy", playlistElement.getPrivacy());
params.put("displayName", playlistElement.getDisplayName()); params.put("displayName", playlistElement.getDisplayName());
httpsConnection.post(getAbsoluteUrl("/video-playlists/"), 60, params, prefKeyOauthTokenT); params.put("privacy", playlistElement.getPrivacy());
actionCode = httpsConnection.getActionCode(); params.put("videoChannelId", playlistElement.getVideoChannelId());
} catch (NoSuchAlgorithmException | IOException | KeyManagementException e) { params.put("description", playlistElement.getDescription());
String response = httpsConnection.postBoundary(getAbsoluteUrl("/video-playlists/"), 60, params, prefKeyOauthTokenT);
playlistId = new JSONObject(response).getJSONObject("videoPlaylist").getString("id");
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
return actionCode; return playlistId;
} }