StreamMediaFileOperation.java 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2018 Tobias Kaminsky
  6. * Copyright (C) 2018 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.files;
  22. import com.owncloud.android.lib.common.OwnCloudClient;
  23. import com.owncloud.android.lib.common.operations.RemoteOperation;
  24. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  25. import com.owncloud.android.lib.common.utils.Log_OC;
  26. import org.apache.commons.httpclient.HttpStatus;
  27. import org.apache.commons.httpclient.methods.PostMethod;
  28. import org.json.JSONObject;
  29. import java.util.ArrayList;
  30. public class StreamMediaFileOperation extends RemoteOperation {
  31. private static final String TAG = StreamMediaFileOperation.class.getSimpleName();
  32. private static final int SYNC_READ_TIMEOUT = 40000;
  33. private static final int SYNC_CONNECTION_TIMEOUT = 5000;
  34. private static final String STREAM_MEDIA_URL = "/ocs/v2.php/apps/dav/api/v1/direct";
  35. private String fileID;
  36. // JSON node names
  37. private static final String NODE_OCS = "ocs";
  38. private static final String NODE_DATA = "data";
  39. private static final String NODE_URL = "url";
  40. private static final String JSON_FORMAT = "?format=json";
  41. public StreamMediaFileOperation(String fileID) {
  42. this.fileID = fileID;
  43. }
  44. protected RemoteOperationResult run(OwnCloudClient client) {
  45. RemoteOperationResult result;
  46. PostMethod postMethod = null;
  47. try {
  48. postMethod = new PostMethod(client.getBaseUri() + STREAM_MEDIA_URL + JSON_FORMAT);
  49. postMethod.setParameter("fileId", fileID);
  50. // remote request
  51. postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
  52. int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);
  53. if (status == HttpStatus.SC_OK) {
  54. String response = postMethod.getResponseBodyAsString();
  55. // Parse the response
  56. JSONObject respJSON = new JSONObject(response);
  57. String url = respJSON.getJSONObject(NODE_OCS).getJSONObject(NODE_DATA).getString(NODE_URL);
  58. result = new RemoteOperationResult(true, postMethod);
  59. ArrayList<Object> urlArray = new ArrayList<>();
  60. urlArray.add(url);
  61. result.setData(urlArray);
  62. } else {
  63. result = new RemoteOperationResult(false, postMethod);
  64. client.exhaustResponse(postMethod.getResponseBodyAsStream());
  65. }
  66. } catch (Exception e) {
  67. result = new RemoteOperationResult(e);
  68. Log_OC.e(TAG, "Get stream url for file with id " + fileID + " failed: " + result.getLogMessage(),
  69. result.getException());
  70. } finally {
  71. if (postMethod != null) {
  72. postMethod.releaseConnection();
  73. }
  74. }
  75. return result;
  76. }
  77. }