Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection path #165

Merged
merged 2 commits into from
Mar 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions appinfo/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
use \OCA\Music\DependencyInjection\DIContainer;


$this->create('music_collection', '/api/collection')->get()->action(
function($params){
App::main('ApiController', 'collection', $params, new DIContainer());
}
);

/**
* Shiva api https://github.com/tooxie/shiva-server#resources
*/
Expand Down
5 changes: 5 additions & 0 deletions appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@
// load file for public sharing page
$api->addScript('public/musicFilePlayer');

// register search provider
\OC_Search::registerProvider('OCA\Music\Utility\Search');

// register settings
$api->registerAdmin('settings/admin');
$api->registerPersonal('settings/user');
4 changes: 4 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ function($params){

// include external API
require_once __DIR__ . '/api.php';

// include settings routes
require_once __DIR__ . '/settings.php';

38 changes: 38 additions & 0 deletions appinfo/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* ownCloud - Music app
*
* @author Morris Jobke
* @copyright 2013 Morris Jobke <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/


namespace OCA\Music;

use \OCA\Music\AppFramework\App;
use \OCA\Music\DependencyInjection\DIContainer;


/**
* Path of the music collection
*/
$this->create('music_settings_user_path', '/settings/user/path')->post()->action(
function($params){
App::main('SettingController', 'userPath', $params, new DIContainer());
}
);
10 changes: 10 additions & 0 deletions businesslayer/albumbusinesslayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public function findAll($userId){
return $this->injectArtists($albums);
}

/**
* Returns all albums
* @param string $userId the name of the user
* @return array of albums
*/
public function findAllWithFileInfo($userId){
$albums = $this->mapper->findAllWithFileInfo($userId);
return $albums;
}

/**
* Returns all albums filtered by artist
* @param string $artistId the id of the artist
Expand Down
10 changes: 10 additions & 0 deletions businesslayer/trackbusinesslayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ public function __construct(TrackMapper $trackMapper, API $api){
parent::__construct($trackMapper, $api);
}

/**
* Returns all tracks filtered by path
* @param string $path the path used as filter
* @param string $userId the name of the user
* @return array of tracks
*/
public function findAllByPath($path, $userId){
return $this->mapper->findAllByPath($path, $userId);
}

/**
* Returns all tracks filtered by artist
* @param string $artistId the id of the artist
Expand Down
48 changes: 48 additions & 0 deletions controller/apicontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,54 @@ public function __construct(API $api, Request $request,
$this->albumBusinessLayer = $albumbusinesslayer;
}

/**
* @CSRFExemption
* @IsAdminExemption
* @IsSubAdminExemption
* @Ajax
* @API
*/
public function collection() {
$userId = $this->api->getUserId();
$path = $this->api->getUserValue('path');
if (!$path) {
$path = '/';
}
$path = 'files' . $path;

$allArtists = $this->artistBusinessLayer->findAll($userId);
$allArtistsById = array();
foreach ($allArtists as &$artist) {
$allArtistsById[$artist->getId()] = $artist->toCollection($this->api);
}

$allAlbums = $this->albumBusinessLayer->findAllWithFileInfo($userId);
$allAlbumsById = array();
foreach ($allAlbums as &$album) {
$allAlbumsById[$album->getId()] = $album->toCollection($this->api);
}

$allTracks = $this->trackBusinessLayer->findAllByPath($path, $userId);

$artists = array();
foreach ($allTracks as $track) {
$artist = &$allArtistsById[$track->getArtistId()];
if (!isset($artist['albums'])) {
$artist['albums'] = array();
$artists[] = &$artist;
}
$album = &$allAlbumsById[$track->getAlbumId()];
if (!isset($album['tracks'])) {
$album['tracks'] = array();
$artist['albums'][] = &$album;
}

$album['tracks'][] = $track->toCollection($this->api);
}

return $this->renderPlainJSON($artists);
}

/**
* @CSRFExemption
* @IsAdminExemption
Expand Down
57 changes: 57 additions & 0 deletions controller/settingcontroller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* ownCloud - Music app
*
* @author Morris Jobke
* @copyright 2013 Morris Jobke <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/


namespace OCA\Music\Controller;

use \OCA\Music\AppFramework\Core\API;
use \OCA\Music\AppFramework\Db\Mapper;
use \OCA\Music\AppFramework\Http\Request;


class SettingController extends Controller {

/**
* @CSRFExemption
* @IsAdminExemption
* @IsSubAdminExemption
* @Ajax
*/
public function userPath() {
$success = false;
$path = $this->params('value');
$pathInfo = $this->api->getFileInfo($path);
if ($pathInfo && $pathInfo['mimetype'] === 'httpd/unix-directory') {
if ($path[0] != '/') {
$path = '/' . $path;
}
if ($path[strlen($path)-1] !== '/') {
$path .= '/';
}
$this->api->setUserValue('path', $path);
$success = true;
}
return $this->renderPlainJSON(array('success' => $success));
}

}
4 changes: 4 additions & 0 deletions css/settings-user.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#music-user input:invalid {
box-shadow: #F00 0px 0px 1.5px 1px;
outline: 0;
}
15 changes: 15 additions & 0 deletions db/album.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Album extends Entity {
public $name;
public $year;
public $coverFileId;
public $coverFilePath;
public $artistIds;
public $artists;
public $userId;
Expand Down Expand Up @@ -75,6 +76,20 @@ public function getNameString(API $api) {
return $name;
}

public function toCollection(API $api) {
$coverUrl = null;
if($this->getCoverFilePath()) {
$coverUrl = $api->linkToRoute('download',
array('file' => strstr($this->getCoverFilePath(),'/')));
}
return array(
'name' => $this->getNameString($api),
'year' => $this->getYear(),
'cover' => $coverUrl,
'id' => $this->getId(),
);
}

public function toAPI(API $api) {
$coverUrl = null;
if($this->getCoverFileId() > 0) {
Expand Down
15 changes: 15 additions & 0 deletions db/albummapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,27 @@ private function makeSelectQuery($condition=null){
'WHERE `album`.`user_id` = ? ' . $condition;
}

private function makeSelectQueryWithFileInfo($condition=null){
return 'SELECT `album`.`name`, `album`.`year`, `album`.`id`, '.
'`album`.`cover_file_id`, `file`.`path` as `coverFilePath`'.
'FROM `*PREFIX*music_albums` `album` '.
'LEFT OUTER JOIN `*PREFIX*filecache` `file` '.
'ON `album`.`cover_file_id` = `file`.`fileid` ' .
'AND `album`.`user_id` = ? ' . $condition;
}

public function findAll($userId){
$sql = $this->makeSelectQuery();
$params = array($userId);
return $this->findEntities($sql, $params);
}

public function findAllWithFileInfo($userId){
$sql = $this->makeSelectQueryWithFileInfo();
$params = array($userId);
return $this->findEntities($sql, $params);
}

public function find($albumId, $userId){
$sql = $this->makeSelectQuery('AND `album`.`id` = ?');
$params = array($userId, $albumId);
Expand Down
7 changes: 7 additions & 0 deletions db/artist.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public function getNameString(API $api) {
return $name;
}

public function toCollection(API $api) {
return array(
'id' => $this->getId(),
'name' => $this->getNameString($api)
);
}

public function toAPI(API $api) {
return array(
'id' => $this->getId(),
Expand Down
15 changes: 15 additions & 0 deletions db/track.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Track extends Entity {
public $length;
public $fileSize;
public $fileId;
public $filePath;
public $bitrate;
public $uri;
public $mimetype;
Expand Down Expand Up @@ -81,6 +82,20 @@ public function getAlbumWithUri(API $api) {
);
}

public function toCollection(API $api) {
return array(
'title' => $this->getTitle(),
'number' => $this->getNumber(),
'artistId' => $this->getArtistId(),
'albumId' => $this->getAlbumId(),
'files' => array($this->getMimetype() => $api->linkToRoute(
'download',
array('file' => strstr($this->getFilePath(),'/'))
)),
'id' => $this->getId(),
);
}

public function toAPI(API $api) {
return array(
'title' => $this->getTitle(),
Expand Down
17 changes: 17 additions & 0 deletions db/trackmapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ private function makeSelectQueryWithoutUserId($condition){
'WHERE ' . $condition;
}

private function makeSelectQueryWithFileInfo($condition){
return 'SELECT `track`.`title`, `track`.`number`, `track`.`id`, '.
'`track`.`artist_id`, `track`.`album_id`, `track`.`length`, '.
'`track`.`file_id`, `track`.`bitrate`, `track`.`mimetype`, '.
'`file`.`path` as `filePath`, `file`.`size` as `fileSize` '.
'FROM `*PREFIX*music_tracks` `track` '.
'INNER JOIN `*PREFIX*filecache` `file` '.
'ON `track`.`file_id` = `file`.`fileid` '.
'WHERE `track`.`user_id` = ? ' . $condition;
}

private function makeSelectQuery($condition=null){
return $this->makeSelectQueryWithoutUserId('`track`.`user_id` = ? ' . $condition);
}
Expand All @@ -50,6 +61,12 @@ public function findAll($userId){
return $this->findEntities($sql, $params);
}

public function findAllByPath($path, $userId){
$sql = $this->makeSelectQueryWithFileInfo('AND `file`.`path` LIKE ?');
$params = array($userId, $path . '%');
return $this->findEntities($sql, $params);
}

public function findAllByArtist($artistId, $userId){
$sql = $this->makeSelectQuery('AND `track`.`artist_id` = ?');
$params = array($userId, $artistId);
Expand Down
5 changes: 5 additions & 0 deletions dependencyinjection/diconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use \OCA\Music\Controller\ApiController;
use \OCA\Music\Controller\LogController;
use \OCA\Music\Controller\PageController;
use \OCA\Music\Controller\SettingController;
use \OCA\Music\Core\API;
use \OCA\Music\DB\AlbumMapper;
use \OCA\Music\DB\ArtistMapper;
Expand Down Expand Up @@ -65,6 +66,10 @@
return new LogController($c['API'], $c['Request']);
});

$this['SettingController'] = $this->share(function($c){
return new SettingController($c['API'], $c['Request']);
});

/**
* Mappers
*/
Expand Down
2 changes: 1 addition & 1 deletion js/app/controllers/maincontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ angular.module('Music').controller('MainController',
};
window.location.hash = '#/' + type + '/' + object.id;
};
}]);
}]);
Loading