Skip to content

Commit

Permalink
More work on endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Petry committed Nov 24, 2014
1 parent 6d85d15 commit 71bb573
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 21 deletions.
20 changes: 17 additions & 3 deletions apps/files/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,23 @@
namespace OCA\Files\Appinfo;

$application = new Application();
$application->registerRoutes($this, array('routes' => array(
array('name' => 'API#getThumbnail', 'url' => '/api/v1/thumbnail/{x}/{y}/{file}', 'verb' => 'GET', 'requirements' => array('file' => '.+')),
)));
$application->registerRoutes(
$this,
array('routes' => array(
array(
'name' => 'API#getThumbnail',
'url' => '/api/v1/thumbnail/{x}/{y}/{file}',
'verb' => 'GET',
'requirements' => array('file' => '.+')
),
array(
'name' => 'API#updateFileMetadata',
'url' => '/api/v1/files/{path}',
'verb' => 'POST',
'requirements' => array('path' => '.+')
),
)
));


/** @var $this \OC\Route\Router */
Expand Down
25 changes: 25 additions & 0 deletions apps/files/controller/apicontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,29 @@ public function getThumbnail($x, $y, $file) {
}
}

/**
* Updates the metadata of the specified file path
*
* @NoAdminRequired
*
* @param string $path path
* @param array $tags array of tags
*/
public function updateFileMetadata($path, $tags) {
$view = new \OC\Files\View('/'.\OCP\User::getUser().'/files');
$fileInfo = $view->getFileInfo($path);
if (!$fileInfo) {
throw new \OCP\Files\NotFoundException();
}

$fileId = $fileInfo->getId();

// TODO: support for other tags
$tagManager = \OC::$server->getTagManager()->load('files');
if (in_array($tagManager::TAG_FAVORITE, $tags)) {
$tagManager->addToFavorites($fileId);
} else {
$tagManager->removeFromFavorites($fileId);
}
}
}
62 changes: 44 additions & 18 deletions apps/files/js/favoritesplugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,38 @@
return $icon;
},
actionHandler: function(fileName, context) {
var $actionEl = context.$file.find('.action-favorite');
var $file = context.$file;
var dir = context.dir || context.fileList.getCurrentDirectory();
self.tag(dir + '/' + fileName, OC.TAG_FAVORITE);
var tags = $file.attr('data-tags');
if (_.isUndefined(tags)) {
console.warn('File has no data-tags attribue');
tags = '';
// TODO: return here as safety (empty string is fine)
}
tags = tags.split('|');
tags = _.without(tags, '');
var isFavorite = tags.indexOf(OC.TAG_FAVORITE) >= 0;
if (isFavorite) {
// remove tag from list
tags = _.without(tags, OC.TAG_FAVORITE);
} else {
tags.push(OC.TAG_FAVORITE);
}
if ($actionEl.hasClass('icon-loading')) {
// do nothing
return;
}
$actionEl.addClass('icon-loading');
self.applyFileTags(
dir + '/' + fileName,
tags
).then(function() {
// TODO: read from result
$actionEl.removeClass('icon-loading');
$actionEl.html(isFavorite ? '☆' : '★');
$file.attr('data-tags', tags.join('|'));
});
}
});

Expand All @@ -83,28 +113,24 @@
},

/**
* Tag the given file or folder.
* Replaces the given files' tags with the specified ones.
*
* @param {String} fileName path to the file or folder to tag
* @param {String} tagName name of the tag
* @param {boolean} [unTag] true to remove the tag, false to add
* @param {Array.<String>} tagNames array of tag names
*/
tag: function(fileName, tagName, unTag) {
// crude ajax for now
var params = {
path: fileName
};
$.ajax({
url: OC.linkToOCS('apps/files/api/v1') + 'tags/' +
encodeURIComponent(tagName) +
OC.buildQueryString(params),
applyFileTags: function(fileName, tagNames) {
var encodedPath = OC.encodePath(fileName);
while (encodedPath[0] === '/') {
encodedPath = encodedPath.substr(1);
}
return $.ajax({
url: OC.generateUrl('/apps/files/api/v1/files/') + encodedPath,
data: {
format: 'json'
format: 'json',
tags: tagNames
},
type: unTag ? 'DELETE' : 'POST',
beforeSend: function(xhr) {
xhr.setRequestHeader('OCS-APIREQUEST', 'true');
}
dataType: 'json',
type: 'POST'
});
}
};
Expand Down
18 changes: 18 additions & 0 deletions core/js/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,24 @@ var OC={
return OC.filePath(app,'img',file);
},

/**
* URI-Encodes a file path but keep the path slashes.
*
* @param path path
* @return encoded path
*/
encodePath: function(path) {
if (!path) {
return path;
}
var parts = path.split('/');
var result = [];
for (var i = 0; i < parts.length; i++) {
result.push(encodeURIComponent(parts[i]));
}
return result.join('/');
},

/**
* Load a script for the server and load it. If the script is already loaded,
* the event handler will be called directly
Expand Down

0 comments on commit 71bb573

Please sign in to comment.