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

Add album fetch routes #4

Merged
merged 10 commits into from
Nov 18, 2019
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
35 changes: 31 additions & 4 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,35 @@
*/

return [
'routes' => [
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
['name' => 'page#index', 'url' => '/{path}', 'verb' => 'GET', 'postfix' => 'folder', 'requirements' => ['path' => '.+']],
]
'routes' => [
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
['name' => 'page#index', 'url' => '/albums', 'verb' => 'GET', 'postfix' => 'albums'],
['name' => 'page#index', 'url' => '/favorites', 'verb' => 'GET', 'postfix' => 'favorites'],
['name' => 'page#index', 'url' => '/shared', 'verb' => 'GET', 'postfix' => 'shared'],
['name' => 'page#index', 'url' => '/tags', 'verb' => 'GET', 'postfix' => 'tags'],

// apis
[
'name' => 'albums#myAlbums',
'url' => '/api/v1/albums/{path}',
'verb' => 'GET',
'requirements' => [
'path' => '.*',
],
'defaults' => [
'path' => '',
],
],
[
'name' => 'albums#sharedAlbums',
'url' => '/api/v1/shared/{path}',
'verb' => 'GET',
'requirements' => [
'path' => '.*',
],
'defaults' => [
'path' => '',
],
],
]
];
161 changes: 161 additions & 0 deletions lib/Controller/AlbumsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Photos\Controller;

use OCA\Files_Sharing\SharedStorage;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\FIles\Node;
use OCP\Files\NotFoundException;
use OCP\IRequest;

class AlbumsController extends Controller {

/** @var string */
private $userId;
/** @var IRootFolder */
private $rootFolder;

public function __construct($appName, IRequest $request, string $userId, IRootFolder $rootFolder) {
parent::__construct($appName, $request);
$this->userId = $userId;
$this->rootFolder = $rootFolder;
}

/**
* @NoAdminRequired
*/
public function myAlbums(string $path = ''): JSONResponse {
return $this->generate($path, false);
}

/**
* @NoAdminRequired
*/
public function sharedAlbums(string $path = ''): JSONResponse {
return $this->generate($path, true);
}

private function generate(string $path, bool $shared): JSONResponse {
$userFolder = $this->rootFolder->getUserFolder($this->userId);

$folder = $userFolder;
if ($path !== '') {
try {
$folder = $userFolder->get($path);
} catch (NotFoundException $e) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}
}

$data = $this->scanCurrentFolder($folder, $shared);
$result = $this->formatData($data);

return new JSONResponse($result, Http::STATUS_OK);
}

private function formatData(iterable $nodes): array {
$userFolder = $this->rootFolder->getUserFolder($this->userId);

$result = [];
/** @var Node $node */
foreach ($nodes as $node) {
// properly format full path and make sure
// we're relative to the user home folder
$isRoot = $node === $userFolder;
$path = $userFolder->getRelativePath($node->getPath());

$result[] = [
'basename' => $isRoot ? '' : $node->getName(),
'etag' => $node->getEtag(),
'fileid' => $node->getId(),
'filename' => $path,
'etag' => $node->getEtag(),
'lastmod' => $node->getMTime(),
'mime' => $node->getMimetype(),
'size' => $node->getSize(),
'type' => $node->getType()
];
}

return $result;
}

private function scanCurrentFolder(Folder $folder, bool $shared): iterable {
$nodes = $folder->getDirectoryListing();

// add current folder to iterable set
yield $folder;

foreach ($nodes as $node) {
if ($node instanceof Folder) {
yield from $this->scanFolder($node, 0, $shared);
} elseif ($node instanceof File) {
if ($this->validFile($node, $shared)) {
yield $node;
}
}
}
}

private function validFile(File $file, bool $shared): bool {
if ($file->getMimePart() === 'image' && $this->isShared($file) === $shared) {
return true;
}

return false;
}

private function isShared(Node $node): bool {
return $node->getStorage()->instanceOfStorage(SharedStorage::class);
}

private function scanFolder(Folder $folder, int $depth, bool $shared): iterable {
if ($depth > 4) {
return [];
}

$nodes = $folder->getDirectoryListing();

foreach ($nodes as $node) {
if ($node instanceof File) {
if ($this->validFile($node, $shared)) {
yield $folder;
return [];
}
}
}

foreach ($nodes as $node) {
if ($node instanceof Folder && $this->isShared($node) === $shared) {
yield from $this->scanFolder($node, $depth + 1, $shared);
}
}
}
}
6 changes: 3 additions & 3 deletions src/components/File.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default {
type: String,
required: true,
},
id: {
fileid: {
type: Number,
required: true,
},
Expand All @@ -87,7 +87,7 @@ export default {
return generateRemoteUrl(`dav/files/${getCurrentUser().uid}`) + this.filename
},
ariaUuid() {
return `image-${this.id}`
return `image-${this.fileid}`
},
ariaLabel() {
return t('photos', 'Open the full size "{name}" image', { name: this.basename })
Expand All @@ -97,7 +97,7 @@ export default {
created() {
// Allow us to cancel the img loading on destroy
// use etag to force cache reload if file changed
this.img.src = generateUrl(`/core/preview?fileId=${this.id}&x=${1024}&y=${1024}&a=true&v=${this.etag}`)
this.img.src = generateUrl(`/core/preview?fileId=${this.fileid}&x=${1024}&y=${1024}&a=true&v=${this.etag}`)
this.img.addEventListener('load', () => {
this.src = this.img.src
})
Expand Down
29 changes: 16 additions & 13 deletions src/components/Folder.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
class="folder-content"
role="none">
<img v-for="file in fileList"
:key="file.id"
:key="file.fileid"
:src="generateImgSrc(file)"
alt=""
@load="loaded = true">
Expand All @@ -54,7 +54,7 @@
import { generateUrl } from '@nextcloud/router'
import { mapGetters } from 'vuex'

import getPictures from '../services/FileList'
import getAlbumContent from '../services/AlbumContent'
import cancelableRequest from '../utils/CancelableRequest'

export default {
Expand All @@ -70,14 +70,18 @@ export default {
type: String,
required: true,
},
id: {
fileid: {
type: Number,
required: true,
},
icon: {
type: String,
default: 'icon-folder',
},
showShared: {
type: Boolean,
default: false,
},
},

data() {
Expand All @@ -96,7 +100,7 @@ export default {

// files list of the current folder
folderContent() {
return this.folders[this.id]
return this.folders[this.fileid]
},
fileList() {
return this.folderContent
Expand All @@ -113,7 +117,7 @@ export default {
},

ariaUuid() {
return `folder-${this.id}`
return `folder-${this.fileid}`
},
ariaLabel() {
return t('photos', 'Open the "{name}" sub-directory', { name: this.basename })
Expand All @@ -137,15 +141,14 @@ export default {

async created() {
// init cancellable request
const { request, cancel } = cancelableRequest(getPictures)
const { request, cancel } = cancelableRequest(getAlbumContent)
this.cancelRequest = cancel

try {
// get data
const { files, folders } = await request(this.filename)
// this.cancelRequest('Stop!')
this.$store.dispatch('updateFolders', { id: this.id, files, folders })
this.$store.dispatch('updateFiles', { folder: this.folder, files, folders })
const { folder, folders, files } = await request(this.filename, {shared: this.showShared})
this.$store.dispatch('updateFolders', { fileid: folder.fileid, files, folders })
this.$store.dispatch('updateFiles', { folder, files, folders })
} catch (error) {
if (error.response && error.response.status) {
console.error('Failed to get folder content', this.folder, error.response)
Expand All @@ -155,13 +158,13 @@ export default {
},

beforeDestroy() {
this.cancelRequest()
this.cancelRequest('Navigated away')
},

methods: {
generateImgSrc({ id, etag }) {
generateImgSrc({ fileid, etag }) {
// use etag to force cache reload if file changed
return generateUrl(`/core/preview?fileId=${id}&x=${256}&y=${256}&a=true&v=${etag}`)
return generateUrl(`/core/preview?fileId=${fileid}&x=${256}&y=${256}&a=true&v=${etag}`)
},

fetch() {
Expand Down
12 changes: 7 additions & 5 deletions src/components/Navigation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ export default {
type: String,
default: t('photos', 'Photos'),
},
id: {
type: Number,
required: true,
},
},

computed: {
Expand Down Expand Up @@ -99,9 +95,15 @@ export default {
* so we generate a new valid route object, get the final url back
* decode it and use it as a direct string, which vue-router
* does not encode afterwards
* @returns {string}
* @returns {string|object}
*/
to() {
if (this.parentPath === '/') {
return { name: this.$route.name }
}

// else let's build the path and make sure it's
// not url encoded (more importantly if filename have slashes)
const route = Object.assign({}, this.$route, {
// always remove first slash
params: { path: this.parentPath.substr(1) },
Expand Down
21 changes: 11 additions & 10 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ import Tags from '../views/Tags'

Vue.use(Router)

// shortcut to properly format the path prop
const props = route => ({
// always lead current path with a slash
path: `/${route.params.path ? route.params.path : ''}`,
})

export default new Router({
mode: 'history',
// if index.php is in the url AND we got this far, then it's working:
Expand All @@ -51,11 +45,14 @@ export default new Router({
path: '/albums',
component: Albums,
name: 'albums',
props,
props: route => ({
// always lead current path with a slash
path: `/${route.params.path ? route.params.path : ''}`,
}),
children: [
{
path: ':path*',
name: 'albumspath',
name: 'albums',
component: Albums,
},
],
Expand All @@ -64,11 +61,15 @@ export default new Router({
path: '/shared',
component: Albums,
name: 'shared',
props,
props: route => ({
// always lead current path with a slash
path: `/${route.params.path ? route.params.path : ''}`,
showShared: true,
}),
children: [
{
path: ':path*',
name: 'sharedpath',
name: 'shared',
component: Albums,
},
],
Expand Down
Loading