Skip to content

Commit

Permalink
api controller
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Röhrl <jakob.roehrl@web.de>
  • Loading branch information
jakobroehrl committed Oct 22, 2020
1 parent f67181d commit 18287c4
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 57 deletions.
29 changes: 29 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* @copyright Copyright (c) 2020 Jakob Röhrl <jakob.roehrl@web.de>
*
* @author Jakob Röhrl <jakob.roehrl@web.de>
*
* @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/>.
*
*/

return [
'routes' => [
// api
['name' => 'Api#getSortInfo', 'url' => '/api/v1/getSortInfo', 'verb' => 'GET'],
]
];
144 changes: 93 additions & 51 deletions js/viewer-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10567,50 +10567,6 @@ createToken('GTE0PRE', '^\\s*>=\\s*0\.0\.0-0\\s*$');

/***/ }),

/***/ "./node_modules/@nextcloud/initial-state/dist/index.js":
/*!*************************************************************!*\
!*** ./node_modules/@nextcloud/initial-state/dist/index.js ***!
\*************************************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


__webpack_require__(/*! core-js/modules/es.array.concat */ "./node_modules/core-js/modules/es.array.concat.js");

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.loadState = loadState;

/**
* @param app app ID, e.g. "mail"
* @param key name of the property
* @param fallback optional parameter to use as default value
* @throws if the key can't be found
*/
function loadState(app, key, fallback) {
var elem = document.querySelector("#initial-state-".concat(app, "-").concat(key));

if (elem === null) {
if (fallback !== undefined) {
return fallback;
}

throw new Error("Could not find initial state ".concat(key, " of ").concat(app));
}

try {
return JSON.parse(atob(elem.value));
} catch (e) {
throw new Error("Could not parse initial state ".concat(key, " of ").concat(app));
}
}
//# sourceMappingURL=index.js.map

/***/ }),

/***/ "./node_modules/@nextcloud/l10n/dist/gettext.js":
/*!******************************************************!*\
!*** ./node_modules/@nextcloud/l10n/dist/gettext.js ***!
Expand Down Expand Up @@ -78846,6 +78802,88 @@ function pushToHistory(_ref) {

/***/ }),

/***/ "./src/services/FilesSort.js":
/*!***********************************!*\
!*** ./src/services/FilesSort.js ***!
\***********************************/
/*! exports provided: default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _nextcloud_axios__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @nextcloud/axios */ "./node_modules/@nextcloud/axios/dist/index.js");
/* harmony import */ var _nextcloud_axios__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_nextcloud_axios__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _nextcloud_router__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @nextcloud/router */ "./node_modules/@nextcloud/router/dist/index.js");
/* harmony import */ var _nextcloud_router__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_nextcloud_router__WEBPACK_IMPORTED_MODULE_1__);
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

/**
* @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @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/>.
*
*/


/* harmony default export */ __webpack_exports__["default"] = (function (_x) {
return _ref.apply(this, arguments);
});

function _ref() {
_ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(obj) {
var sortInfo, url;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
sortInfo = '';
_context.prev = 1;
url = Object(_nextcloud_router__WEBPACK_IMPORTED_MODULE_1__["generateUrl"])('/apps/viewer/api/v1/getSortInfo');
_context.next = 5;
return _nextcloud_axios__WEBPACK_IMPORTED_MODULE_0___default.a.get(url);

case 5:
sortInfo = _context.sent;
_context.next = 11;
break;

case 8:
_context.prev = 8;
_context.t0 = _context["catch"](1);
console.error(_context.t0);

case 11:
return _context.abrupt("return", sortInfo);

case 12:
case "end":
return _context.stop();
}
}
}, _callee, null, [[1, 8]]);
}));
return _ref.apply(this, arguments);
}

/***/ }),

/***/ "./src/services/Viewer.js":
/*!********************************!*\
!*** ./src/services/Viewer.js ***!
Expand Down Expand Up @@ -79255,8 +79293,7 @@ __webpack_require__.r(__webpack_exports__);
/* harmony import */ var camelcase__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! camelcase */ "./node_modules/camelcase/index.js");
/* harmony import */ var camelcase__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(camelcase__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _numberUtil__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./numberUtil */ "./src/utils/numberUtil.js");
/* harmony import */ var _nextcloud_initial_state__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @nextcloud/initial-state */ "./node_modules/@nextcloud/initial-state/dist/index.js");
/* harmony import */ var _nextcloud_initial_state__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_nextcloud_initial_state__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _services_FilesSort__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../services/FilesSort */ "./src/services/FilesSort.js");
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

/**
Expand Down Expand Up @@ -79327,10 +79364,15 @@ var extractFilePaths = function extractFilePaths(path) {

var sortCompare = function sortCompare(fileInfo1, fileInfo2, key) {
var asc = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
var fileSorting = Object(_nextcloud_initial_state__WEBPACK_IMPORTED_MODULE_2__["loadState"])('viewer', 'file-sorting');
var fileSortingDirection = Object(_nextcloud_initial_state__WEBPACK_IMPORTED_MODULE_2__["loadState"])('viewer', 'file-sorting-direction');
console.log(fileSorting);
console.log(fileSortingDirection);

/*
fileSorting: asc, desc​
fileSortingDirection: name, mtime, size
*/
var sortdata = '';
Object(_services_FilesSort__WEBPACK_IMPORTED_MODULE_2__["default"])().then(function (ret) {
sortdata = ret.data;
});

if (fileInfo1.isFavorite && !fileInfo2.isFavorite) {
return -1;
Expand Down Expand Up @@ -79562,4 +79604,4 @@ __webpack_require__.r(__webpack_exports__);
/***/ })

/******/ });
//# sourceMappingURL=viewer-main.js.map?v=e57afc0cd7352e206277
//# sourceMappingURL=viewer-main.js.map?v=f836fa91f00935d46af8
2 changes: 1 addition & 1 deletion js/viewer-main.js.map

Large diffs are not rendered by default.

79 changes: 79 additions & 0 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Jakob Röhrl <jakob.roehrl@web.de>
*
* @author Jakob Röhrl <jakob.roehrl@web.de>
*
* @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\Viewer\Controller;

use OCA\Viewer\AppInfo\Application;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;

class ApiController extends Controller {

/** @var IConfig */
private $config;

/** @var IUserSession */
private $userSession;

public function __construct(IRequest $request,
IConfig $config,
IUserSession $userSession) {
parent::__construct(Application::APP_ID, $request);

$this->config = $config;
$this->userSession = $userSession;
}

/**
* @NoAdminRequired
*
* update preferences (user setting)
*
* @param string key the identifier to change
* @param string value the value to set
*
* @return JSONResponse an empty JSONResponse with respective http status code
*/
public function getSortInfo(): JSONResponse {

$fileSorting = 'name';
$fileSortingDirection = 'asc';

$user = $this->userSession->getUser();
if ($user !== null) {
$fileSorting = $this->config->getUserValue($user->getUID(), 'files', 'file_sorting', 'name');
$fileSortingDirection = $this->config->getUserValue($user->getUID(), 'files', 'file_sorting_direction', 'asc');
}

return new JSONResponse([
'fileSorting' => $fileSorting,
'fileSortingDirection' => $fileSortingDirection,
], Http::STATUS_OK);
}
}
35 changes: 35 additions & 0 deletions src/services/FilesSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @copyright Copyright (c) 2019 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @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/>.
*
*/

import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'

export default async function(obj) {
let sortInfo = ''
try {
const url = generateUrl('/apps/viewer/api/v1/getSortInfo')
sortInfo = await axios.get(url)
} catch (error) {
console.error(error)
}
return sortInfo
}
14 changes: 9 additions & 5 deletions src/utils/fileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
import camelcase from 'camelcase'
import { isNumber } from './numberUtil'
import { loadState } from '@nextcloud/initial-state'
import sortInfo from '../services/FilesSort'

/**
* Get an url encoded path
Expand Down Expand Up @@ -64,11 +64,15 @@ const extractFilePaths = function(path) {
*/
const sortCompare = function(fileInfo1, fileInfo2, key, asc = true) {

const fileSorting = loadState('viewer', 'file-sorting')
const fileSortingDirection = loadState('viewer', 'file-sorting-direction')
/*
fileSorting: asc, desc
fileSortingDirection: name, mtime, size
*/

console.log(fileSorting)
console.log(fileSortingDirection)
let sortdata = ''
sortInfo().then((ret) => {
sortdata = ret.data
})

if (fileInfo1.isFavorite && !fileInfo2.isFavorite) {
return -1
Expand Down

0 comments on commit 18287c4

Please sign in to comment.