Skip to content

Commit

Permalink
Build the albumMap even when the server returns a 403
Browse files Browse the repository at this point in the history
The albumMap is used for navigation, so it's important to build it whenever the user enters the app
  • Loading branch information
oparoz committed Mar 16, 2016
1 parent 9555d9c commit a7d7775
Show file tree
Hide file tree
Showing 19 changed files with 816 additions and 459 deletions.
45 changes: 26 additions & 19 deletions controller/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ trait Files {
/**
* @NoAdminRequired
*
* Returns a list of all media files available to the authenticated user
* Returns a list of all media files and albums available to the authenticated user
*
* * Authentication can be via a login/password or a token/(password)
* * For private galleries, it returns all media files, with the full path from the root
Expand All @@ -64,22 +64,24 @@ trait Files {
*
* @return array <string,array<string,string|int>>|Http\JSONResponse
*/
private function getFiles($location, $features, $etag, $mediatypes) {
private function getFilesAndAlbums($location, $features, $etag, $mediatypes) {
$files = [];
$albums = [];
$updated = true;
/** @var Folder $folderNode */
list($folderPathFromRoot, $folderNode, $locationHasChanged) =
list($folderPathFromRoot, $folderNode) =
$this->searchFolderService->getCurrentFolder(rawurldecode($location), $features);
$albumInfo =
$this->configService->getAlbumInfo($folderNode, $folderPathFromRoot, $features);

if ($albumInfo['etag'] !== $etag) {
$files = $this->searchMediaService->getMediaFiles(
$albumConfig = $this->configService->getConfig($folderNode, $features);
if ($folderNode->getEtag() !== $etag) {
list($files, $albums) = $this->searchMediaService->getMediaFiles(
$folderNode, $mediatypes, $features
);
$files = $this->fixPaths($files, $folderPathFromRoot);
} else {
$updated = false;
}
$files = $this->fixPaths($files, $folderPathFromRoot);

return $this->formatResults($files, $albumInfo, $locationHasChanged);
return $this->formatResults($files, $albums, $albumConfig, $folderPathFromRoot, $updated);
}

/**
Expand All @@ -90,8 +92,8 @@ private function getFiles($location, $features, $etag, $mediatypes) {
* becomes
* /root/folder/file.ext
*
* @param $files
* @param $folderPathFromRoot
* @param array $files
* @param string $folderPathFromRoot
*
* @return array
*/
Expand All @@ -109,17 +111,22 @@ private function fixPaths($files, $folderPathFromRoot) {
* Simply builds and returns an array containing the list of files, the album information and
* whether the location has changed or not
*
* @param array <string,string|int> $files
* @param array $albumInfo
* @param bool $locationHasChanged
* @param array $files
* @param array $albums
* @param array $albumConfig
* @param string $folderPathFromRoot
* @param bool $updated
*
* @return array
* @internal param $array <string,string|int> $files
*/
private function formatResults($files, $albumInfo, $locationHasChanged) {
private function formatResults($files, $albums, $albumConfig, $folderPathFromRoot, $updated) {
return [
'files' => $files,
'albuminfo' => $albumInfo,
'locationhaschanged' => $locationHasChanged
'files' => $files,
'albums' => $albums,
'albumconfig' => $albumConfig,
'albumpath' => $folderPathFromRoot,
'updated' => $updated
];
}

Expand Down
2 changes: 1 addition & 1 deletion controller/filesapicontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function getList($location, $features, $etag, $mediatypes) {
$featuresArray = explode(';', $features);
$mediaTypesArray = explode(';', $mediatypes);
try {
return $this->getFiles($location, $featuresArray, $etag, $mediaTypesArray);
return $this->getFilesAndAlbums($location, $featuresArray, $etag, $mediaTypesArray);
} catch (\Exception $exception) {
return $this->jsonError($exception);
}
Expand Down
2 changes: 1 addition & 1 deletion controller/filescontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function getList($location, $features, $etag, $mediatypes) {
$featuresArray = explode(';', $features);
$mediaTypesArray = explode(';', $mediatypes);
try {
return $this->getFiles($location, $featuresArray, $etag, $mediaTypesArray);
return $this->getFilesAndAlbums($location, $featuresArray, $etag, $mediaTypesArray);
} catch (\Exception $exception) {
return $this->jsonError($exception);
}
Expand Down
2 changes: 1 addition & 1 deletion environment/environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public function getPathFromUserFolder($file) {
*
* That root folder changes when folders are shared publicly
*
* @param File|Folder $node
* @param File|Folder|N $node
*
* @return string
*/
Expand Down
111 changes: 74 additions & 37 deletions js/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,34 @@
};
// Only use the folder as a GET parameter and not as part of the URL
var url = Gallery.utility.buildGalleryUrl('files', '/list', params);
return $.getJSON(url).then(function (/**{{albuminfo:Object, files:Array}}*/ data) {
/**@type {{
* fileid: number,
* permissions: number,
* path: string,
* etag: string
* information,
* sorting,
* error: string
* }}*/
var albumInfo = data.albuminfo;
Gallery.config.setAlbumConfig(albumInfo);
// Both the folder and the etag have to match
if ((decodeURIComponent(currentLocation) === albumInfo.path)
&& (albumInfo.etag === albumEtag)) {
Gallery.imageMap = albumCache.imageMap;
} else {
Gallery._mapFiles(data);
}
return $.getJSON(url).then(
function (/**@type{{
* files:Array,
* albums:Array,
* albumconfig:Object,
* albumpath:String,
* updated:Boolean}}*/
data) {
var albumpath = data.albumpath;
var updated = data.updated;
// FIXME albumConfig should be cached as well
/**@type {{design,information,sorting,error: string}}*/
var albumConfig = data.albumconfig;
//Gallery.config.setAlbumPermissions(currentAlbum);
Gallery.config.setAlbumConfig(albumConfig, albumpath);
// Both the folder and the etag have to match
if ((decodeURIComponent(currentLocation) === albumpath) &&
(updated === false)) {
Gallery.imageMap = albumCache.imageMap;
} else {
Gallery._mapFiles(data);
}

// Restore the previous sorting order for this album
if (!$.isEmptyObject(Gallery.albumMap[albumInfo.path].sorting)) {
Gallery.config.updateAlbumSorting(Gallery.albumMap[albumInfo.path].sorting);
}
// Restore the previous sorting order for this album
if (!$.isEmptyObject(Gallery.albumMap[albumpath].sorting)) {
Gallery.config.updateAlbumSorting(
Gallery.albumMap[albumpath].sorting);
}

}, function (xhr) {
var result = xhr.responseJSON;
Expand Down Expand Up @@ -183,9 +187,12 @@
event.preventDefault();
event.stopPropagation();

var albumPermissions = Gallery.config.albumPermissions;
$('a.share').data('path', albumPermissions.path).data('link', true)
.data('possible-permissions', albumPermissions.permissions).click();
var currentAlbum = Gallery.albumMap[Gallery.currentAlbum];
$('a.share').data('path', currentAlbum.path)
.data('link', true)
.data('item-source', currentAlbum.fileId)
.data('possible-permissions', currentAlbum.permissions)
.click();
if (!$('#linkCheckbox').is(':checked')) {
$('#linkText').hide();
}
Expand Down Expand Up @@ -360,7 +367,13 @@
/**
* Builds the album's model
*
* @param {{albuminfo:Object, files:Array}} data
* @param {{
* files:Array,
* albums:Array,
* albumconfig:Object,
* albumpath:String,
* updated:Boolean
* }} data
* @private
*/
_mapFiles: function (data) {
Expand All @@ -373,23 +386,37 @@
var etag = null;
var size = null;
var sharedWithUser = null;
var albumInfo = data.albuminfo;
var currentLocation = albumInfo.path;
var owner = null;
var currentLocation = data.albumpath;
// This adds a new node to the map for each parent album
Gallery._mapStructure(currentLocation);
var files = data.files;
if (files.length > 0) {
var subAlbumCache = {};
var albumCache = Gallery.albumMap[currentLocation]
= new Album(currentLocation, [], [], OC.basename(currentLocation));
= new Album(
currentLocation,
[],
[],
OC.basename(currentLocation),
data.albums[currentLocation].nodeid,
data.albums[currentLocation].mtime,
data.albums[currentLocation].etag,
data.albums[currentLocation].size,
data.albums[currentLocation].sharedwithuser,
data.albums[currentLocation].owner,
data.albums[currentLocation].freespace,
data.albums[currentLocation].permissions
);
for (var i = 0; i < files.length; i++) {
path = files[i].path;
fileId = files[i].fileid;
fileId = files[i].nodeid;
mimeType = files[i].mimetype;
mTime = files[i].mtime;
etag = files[i].etag;
size = files[i].size;
sharedWithUser = files[i].sharedWithUser;
sharedWithUser = files[i].sharedwithuser;
owner = files[i].owner;

image =
new GalleryImage(
Expand All @@ -408,11 +435,21 @@
// The image belongs to a sub-album, so we create a sub-album cache if it
// doesn't exist and add images to it
if (!subAlbumCache[dir]) {
subAlbumCache[dir] = new Album(dir, [], [],
OC.basename(dir));
subAlbumCache[dir] = new Album(
dir,
[],
[],
OC.basename(dir),
data.albums[dir].nodeid,
data.albums[dir].mtime,
data.albums[dir].etag,
data.albums[dir].size,
data.albums[dir].sharedwithuser,
data.albums[currentLocation].owner,
data.albums[currentLocation].freespace,
data.albums[dir].permissions);
}
subAlbumCache[dir].images.push(image);

// The sub-album also has to be added to the global map
if (!Gallery.albumMap[dir]) {
Gallery.albumMap[dir] = {};
Expand All @@ -424,13 +461,13 @@
Gallery._mapAlbums(albumCache, subAlbumCache);

// Caches the information which is not already cached
albumCache.etag = albumInfo.etag;
albumCache.etag = data.albums[currentLocation].etag;
albumCache.imageMap = Gallery.imageMap;
}
},

/**
* Adds every album leading the current folder to a global album map
* Adds every album leading to the current folder to a global album map
*
* Per example, if you have Root/Folder1/Folder2/CurrentFolder then the map will contain:
* * Root
Expand Down
36 changes: 29 additions & 7 deletions js/galleryalbum.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
var TEMPLATE =
'<a class="row-element" style="width: {{targetWidth}}px; height: {{targetHeight}}px;" ' +
'data-width="{{targetWidth}}" data-height="{{targetHeight}}"' +
'href="{{targetPath}}" data-dir="{{dir}}" data-path="{{path}}">' +
'href="{{targetPath}}" data-dir="{{dir}}" data-path="{{path}}"' +
'data-permissions="{{permissions}}" data-freespace="{{freeSpace}}"' +
'>' +
' <div class="album-loader loading"></div>' +
' <span class="album-label">' +
' <span class="title">{{label}}</span>' +
Expand All @@ -21,14 +23,31 @@
* @param {Array<Album|GalleryImage>} subAlbums
* @param {Array<Album|GalleryImage>} images
* @param {string} name
* @param {number} fileId
* @param {number} mTime
* @param {string} etag
* @param {number} size
* @param {Boolean} sharedWithUser
* @param {string} owner
* @param {number} freeSpace
* @param {number} permissions
* @constructor
*/
var Album = function (path, subAlbums, images, name) {
var Album = function (path, subAlbums, images, name, fileId, mTime, etag, size, sharedWithUser,
owner, freeSpace, permissions) {
this.path = path;
this.subAlbums = subAlbums;
this.images = images;
this.viewedItems = 0;
this.name = name;
this.fileId = fileId;
this.mTime = mTime;
this.etag = etag;
this.size = size;
this.sharedWithUser = sharedWithUser;
this.owner = owner;
this.freeSpace = freeSpace;
this.permissions = permissions;
this.domDef = null;
this.loader = null;
this.preloadOffset = 0;
Expand Down Expand Up @@ -96,6 +115,8 @@
targetWidth: targetHeight,
dir: this.path,
path: this.path,
permissions: this.permissions,
freeSpace: this.freeSpace,
label: this.name,
targetPath: '#' + encodeURIComponent(this.path)
});
Expand Down Expand Up @@ -308,10 +329,11 @@
var subAlbum = this.domDef.children('.album');

if (this.images.length >= 1) {
this._getFourImages(this.images, targetHeight, subAlbum).fail(function (validImages) {
album.images = validImages;
album._fillSubAlbum(targetHeight, subAlbum);
});
this._getFourImages(this.images, targetHeight, subAlbum).fail(
function (validImages) {
album.images = validImages;
album._fillSubAlbum(targetHeight, subAlbum);
});
} else {
var imageHolder = $('<div class="cropped">');
subAlbum.append(imageHolder);
Expand All @@ -328,7 +350,7 @@
*/
_showFolder: function (targetHeight, imageHolder) {
var image = new GalleryImage('Generic folder', 'Generic folder', -1, 'image/svg+xml',
null,null);
null, null);
var thumb = Thumbnails.getStandardIcon(-1);
image.thumbnail = thumb;
this.images.push(image);
Expand Down
Loading

0 comments on commit a7d7775

Please sign in to comment.