StreamMediaFileOperation.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Nextcloud - Android Client
  3. *
  4. * SPDX-FileCopyrightText: 2018 Tobias Kaminsky <tobias@kaminsky.me>
  5. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH
  6. * SPDX-License-Identifier: AGPL-3.0-or-later
  7. */
  8. package com.owncloud.android.files;
  9. import com.owncloud.android.lib.common.OwnCloudClient;
  10. import com.owncloud.android.lib.common.operations.RemoteOperation;
  11. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  12. import com.owncloud.android.lib.common.utils.Log_OC;
  13. import org.apache.commons.httpclient.HttpStatus;
  14. import org.apache.commons.httpclient.methods.Utf8PostMethod;
  15. import org.json.JSONObject;
  16. import java.util.ArrayList;
  17. public class StreamMediaFileOperation extends RemoteOperation {
  18. private static final String TAG = StreamMediaFileOperation.class.getSimpleName();
  19. private static final int SYNC_READ_TIMEOUT = 40000;
  20. private static final int SYNC_CONNECTION_TIMEOUT = 5000;
  21. private static final String STREAM_MEDIA_URL = "/ocs/v2.php/apps/dav/api/v1/direct";
  22. private final long fileID;
  23. // JSON node names
  24. private static final String NODE_OCS = "ocs";
  25. private static final String NODE_DATA = "data";
  26. private static final String NODE_URL = "url";
  27. private static final String JSON_FORMAT = "?format=json";
  28. public StreamMediaFileOperation(long fileID) {
  29. this.fileID = fileID;
  30. }
  31. protected RemoteOperationResult run(OwnCloudClient client) {
  32. RemoteOperationResult result;
  33. Utf8PostMethod postMethod = null;
  34. try {
  35. postMethod = new Utf8PostMethod(client.getBaseUri() + STREAM_MEDIA_URL + JSON_FORMAT);
  36. postMethod.setParameter("fileId", String.valueOf(fileID));
  37. // remote request
  38. postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
  39. int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
  40. if (status == HttpStatus.SC_OK) {
  41. String response = postMethod.getResponseBodyAsString();
  42. // Parse the response
  43. JSONObject respJSON = new JSONObject(response);
  44. String url = respJSON.getJSONObject(NODE_OCS).getJSONObject(NODE_DATA).getString(NODE_URL);
  45. result = new RemoteOperationResult(true, postMethod);
  46. ArrayList<Object> urlArray = new ArrayList<>();
  47. urlArray.add(url);
  48. result.setData(urlArray);
  49. } else {
  50. result = new RemoteOperationResult(false, postMethod);
  51. client.exhaustResponse(postMethod.getResponseBodyAsStream());
  52. }
  53. } catch (Exception e) {
  54. result = new RemoteOperationResult(e);
  55. Log_OC.e(TAG, "Get stream url for file with id " + fileID + " failed: " + result.getLogMessage(),
  56. result.getException());
  57. } finally {
  58. if (postMethod != null) {
  59. postMethod.releaseConnection();
  60. }
  61. }
  62. return result;
  63. }
  64. }