Skip to content

Commit

Permalink
Use relative instead of absolute URLs to play tracks
Browse files Browse the repository at this point in the history
Previously, the front-end requested webdav URL from the back-end and
the latter returned an absolute URL which was then used for playing.
This caused problems with some untypical proxy configurations, as it is
possible that the protocol (HTTP/HTTPS) seen by the back-end is not the
same as the one used by the front-end. Attempting to play content over
HTTP on a page otherwise using HTTPS is blocked by browsers for security
reasons.

Now the back-end returns only a relative path of the file. It's up to
the front-end to build the full webdav URL out of this. Furthermore,
the URL built by the front-end is also relative since there is no
reason for it to be absolute. The solution is inspired by the one used
in the Files app.

Fixes #650
  • Loading branch information
paulijar committed Jun 26, 2018
1 parent bda2b7a commit 068c5f8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ Beside those mentioned resources following additional resources are implemented:
* `/api/cover/{hash}`
* `/api/file/{fileId}`
* `/api/file/{fileId}/info`
* `/api/file/{fileId}/webdav`
* `/api/file/{fileId}/path`
* `/api/file/{fileId}/download`
* `/api/scan`
* `/api/scanstate`
Expand Down Expand Up @@ -259,7 +259,7 @@ Response:

### `/api/collection`

Returns all artists with nested albums and each album with nested tracks. The tracks carry file IDs which can be used to obtain WebDAV link for playing with /api/file/{fileId}/webdav.
Returns all artists with nested albums and each album with nested tracks. Each track carries a file ID which can be used to obtain the file path with `/api/file/{fileId}/path`. The front-end converts the path into playable WebDAV link like this: `OC.linkToRemoteBase('webdav') + path`.

GET /api/collection

Expand Down
3 changes: 2 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
['name' => 'api#prepareCollection', 'url' => '/api/prepare_collection', 'verb' => 'POST'],
['name' => 'api#collection', 'url' => '/api/collection', 'verb' => 'GET'],
['name' => 'api#trackByFileId', 'url' => '/api/file/{fileId}', 'verb' => 'GET'],
['name' => 'api#fileWebDavUrl', 'url' => '/api/file/{fileId}/webdav', 'verb' => 'GET'],
['name' => 'api#fileWebDavUrl', 'url' => '/api/file/{fileId}/webdav', 'verb' => 'GET'], // DEPRECATED
['name' => 'api#filePath', 'url' => '/api/file/{fileId}/path', 'verb' => 'GET'],
['name' => 'api#download', 'url' => '/api/file/{fileId}/download', 'verb' => 'GET'],
['name' => 'api#fileInfo', 'url' => '/api/file/{fileId}/info', 'verb' => 'GET'],
['name' => 'api#getScanState', 'url' => '/api/scanstate', 'verb' => 'GET'],
Expand Down
18 changes: 18 additions & 0 deletions controller/apicontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,24 @@ public function trackByFileId($fileId) {
* @NoAdminRequired
* @NoCSRFRequired
*/
public function filePath($fileId) {
$nodes = $this->userFolder->getById($fileId);
if (\count($nodes) == 0) {
return new ErrorResponse(Http::STATUS_NOT_FOUND);
} else {
$node = $nodes[0];
$path = $this->userFolder->getRelativePath($node->getPath());
// URL encode each part of the file path
$path = \join('/', \array_map('rawurlencode', \explode('/', $path)));
return new JSONResponse(['path' => $path]);
}
}

/**
* @NoAdminRequired
* @NoCSRFRequired
* DEPRECATED
*/
public function fileWebDavUrl($fileId) {
$nodes = $this->userFolder->getById($fileId);
if (\count($nodes) == 0) {
Expand Down
5 changes: 3 additions & 2 deletions js/app/controllers/playercontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ function ($scope, $rootScope, playlistService, libraryService,

// get webDAV URL to the track and start playing it
var mimeAndId = $scope.getPlayableFileId(track);
Restangular.one('file', mimeAndId.id).one('webdav').get().then(function(result) {
Restangular.one('file', mimeAndId.id).one('path').get().then(function(result) {
// It is possible that the active track has already changed again by the time we get
// the URI. Do not start playback in that case.
if (track == $scope.currentTrack) {
var url = result.url + '?requesttoken=' + encodeURIComponent(OC.requestToken);
var url = OC.linkToRemoteBase('webdav') + result.path +
'?requesttoken=' + encodeURIComponent(OC.requestToken);
$scope.player.fromURL(url, mimeAndId.mime);
$scope.seekCursorType = $scope.player.seekingSupported() ? 'pointer' : 'default';

Expand Down
5 changes: 3 additions & 2 deletions js/public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,11 +681,12 @@ function ($scope, $rootScope, playlistService, libraryService,

// get webDAV URL to the track and start playing it
var mimeAndId = $scope.getPlayableFileId(track);
Restangular.one('file', mimeAndId.id).one('webdav').get().then(function(result) {
Restangular.one('file', mimeAndId.id).one('path').get().then(function(result) {
// It is possible that the active track has already changed again by the time we get
// the URI. Do not start playback in that case.
if (track == $scope.currentTrack) {
var url = result.url + '?requesttoken=' + encodeURIComponent(OC.requestToken);
var url = OC.linkToRemoteBase('webdav') + result.path +
'?requesttoken=' + encodeURIComponent(OC.requestToken);
$scope.player.fromURL(url, mimeAndId.mime);
$scope.seekCursorType = $scope.player.seekingSupported() ? 'pointer' : 'default';

Expand Down

0 comments on commit 068c5f8

Please sign in to comment.