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 7cd689b
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 75 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'],
]
];
229 changes: 161 additions & 68 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,10 +79293,13 @@ __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); }

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>
*
Expand Down Expand Up @@ -79325,34 +79366,86 @@ 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);
var sortCompare = /*#__PURE__*/function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(fileInfo1, fileInfo2, key) {
var asc,
sortdata,
_args = arguments;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
asc = _args.length > 3 && _args[3] !== undefined ? _args[3] : true;
_context.prev = 1;
_context.next = 4;
return Object(_services_FilesSort__WEBPACK_IMPORTED_MODULE_2__["default"])();

case 4:
sortdata = _context.sent;
console.log(sortdata.data);
_context.next = 11;
break;

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

if (fileInfo1.isFavorite && !fileInfo2.isFavorite) {
return -1;
} else if (!fileInfo1.isFavorite && fileInfo2.isFavorite) {
return 1;
} // if this is a number, let's sort by integer
case 11:
if (!(fileInfo1.isFavorite && !fileInfo2.isFavorite)) {
_context.next = 15;
break;
}

return _context.abrupt("return", -1);

if (Object(_numberUtil__WEBPACK_IMPORTED_MODULE_1__["isNumber"])(fileInfo1[key]) && Object(_numberUtil__WEBPACK_IMPORTED_MODULE_1__["isNumber"])(fileInfo2[key])) {
return Number(fileInfo1[key]) - Number(fileInfo2[key]);
} // else we sort by string, so let's sort directories first
case 15:
if (!(!fileInfo1.isFavorite && fileInfo2.isFavorite)) {
_context.next = 17;
break;
}

return _context.abrupt("return", 1);

if (fileInfo1.type === 'directory' && fileInfo2.type !== 'directory') {
return -1;
} else if (fileInfo1.type !== 'directory' && fileInfo2.type === 'directory') {
return 1;
} // finally sort by name
case 17:
if (!(Object(_numberUtil__WEBPACK_IMPORTED_MODULE_1__["isNumber"])(fileInfo1[key]) && Object(_numberUtil__WEBPACK_IMPORTED_MODULE_1__["isNumber"])(fileInfo2[key]))) {
_context.next = 19;
break;
}

return _context.abrupt("return", Number(fileInfo1[key]) - Number(fileInfo2[key]));

return asc ? fileInfo1[key].localeCompare(fileInfo2[key], OC.getLanguage()) : -fileInfo1[key].localeCompare(fileInfo2[key], OC.getLanguage());
};
case 19:
if (!(fileInfo1.type === 'directory' && fileInfo2.type !== 'directory')) {
_context.next = 23;
break;
}

return _context.abrupt("return", -1);

case 23:
if (!(fileInfo1.type !== 'directory' && fileInfo2.type === 'directory')) {
_context.next = 25;
break;
}

return _context.abrupt("return", 1);

case 25:
return _context.abrupt("return", asc ? fileInfo1[key].localeCompare(fileInfo2[key], OC.getLanguage()) : -fileInfo1[key].localeCompare(fileInfo2[key], OC.getLanguage()));

case 26:
case "end":
return _context.stop();
}
}
}, _callee, null, [[1, 8]]);
}));

return function sortCompare(_x, _x2, _x3) {
return _ref.apply(this, arguments);
};
}();
/**
* Generate a fileinfo object based on the full dav properties
* It will flatten everything and put all keys to camelCase
Expand Down Expand Up @@ -79562,4 +79655,4 @@ __webpack_require__.r(__webpack_exports__);
/***/ })

/******/ });
//# sourceMappingURL=viewer-main.js.map?v=e57afc0cd7352e206277
//# sourceMappingURL=viewer-main.js.map?v=e1a476f9be90774a353e
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);
}
}
Loading

0 comments on commit 7cd689b

Please sign in to comment.