Skip to content

Commit

Permalink
Merge pull request #2392 from nextcloud/refactor/use-files-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv authored Aug 9, 2024
2 parents 7724837 + 1dea2c8 commit 7417482
Show file tree
Hide file tree
Showing 15 changed files with 9,411 additions and 196 deletions.
9,334 changes: 9,334 additions & 0 deletions css/main-DduclpYy.chunk.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion css/viewer-main.css
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/* extracted by css-entry-points-plugin */
@import './main-DbVYYLtT.chunk.css';
@import './main-DduclpYy.chunk.css';
122 changes: 44 additions & 78 deletions js/viewer-main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11038,21 +11038,21 @@ const onError = async (error) => {
return Promise.reject(error);
};
var _a;
const client = axios.create({
const client$1 = axios.create({
headers: {
requesttoken: (_a = d$3()) != null ? _a : "",
"X-Requested-With": "XMLHttpRequest"
}
});
const cancelableClient = Object.assign(client, {
const cancelableClient = Object.assign(client$1, {
CancelToken: axios.CancelToken,
isCancel: axios.isCancel
});
cancelableClient.interceptors.response.use((r2) => r2, onError$2(cancelableClient));
cancelableClient.interceptors.response.use((r2) => r2, onError$1(cancelableClient));
cancelableClient.interceptors.response.use((r2) => r2, onError);
c$2((token) => {
client.defaults.headers.requesttoken = token;
client$1.defaults.headers.requesttoken = token;
});
function assertPath(path) {
if (typeof path !== "string") {
Expand Down Expand Up @@ -25845,8 +25845,7 @@ r$1.hT;
r$1.O4;
r$1.Kd;
r$1.YK;
var an = r$1.UU;
r$1.Gu;
var an = r$1.UU, un = r$1.Gu;
r$1.ky;
r$1.h4;
r$1.ch;
Expand Down Expand Up @@ -26517,15 +26516,39 @@ function davGetRootPath() {
}
return `/files/${l$2()?.uid}`;
}
davGetRootPath();
const davRootPath = davGetRootPath();
function davGetRemoteURL() {
const url = U$2("dav");
if (isPublicShare()) {
return url.replace("remote.php", "public.php");
}
return url;
}
davGetRemoteURL();
const davRemoteURL = davGetRemoteURL();
const davGetClient = function(remoteURL = davRemoteURL, headers = {}) {
const client2 = an(remoteURL, { headers });
function setHeaders(token) {
client2.setHeaders({
...headers,
// Add this so the server knows it is an request from the browser
"X-Requested-With": "XMLHttpRequest",
// Inject user auth
requesttoken: token ?? ""
});
}
c$2(setHeaders);
setHeaders(d$3());
const patcher = un();
patcher.patch("fetch", (url, options2) => {
const headers2 = options2.headers;
if (headers2?.method) {
options2.method = headers2.method;
delete headers2.method;
}
return fetch(url, options2);
});
return client2;
};
var util$3 = {};
(function(exports) {
const nameStartChar = ":A-Za-z_\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD";
Expand Down Expand Up @@ -26698,48 +26721,6 @@ var re$1 = { exports: {} };
createToken("GTE0PRE", "^\\s*>=\\s*0\\.0\\.0-0\\s*$");
})(re$1, re$1.exports);
/*! third party licenses: js/vendor.LICENSE.txt */
/**
* @copyright Copyright (c) 2019 John Molakvoæ <[email protected]>
*
* @author John Molakvoæ <[email protected]>
*
* @license AGPL-3.0-or-later
*
* 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/>.
*
*/
const getRootPath = function() {
if (!isPublic()) {
return U$2(`dav${getUserRoot()}`);
} else {
return U$2("webdav").replace("/remote.php", "/public.php");
}
};
const getUserRoot = function() {
if (isPublic()) {
throw new Error("No user logged in");
}
return `/files/${l$2()?.uid}`;
};
const isPublic = function() {
return !l$2();
};
const getToken = function() {
const tokenInput = document.getElementById("sharingToken");
return tokenInput && tokenInput.value;
};
/*! third party licenses: js/vendor.LICENSE.txt */
/**
* @copyright Copyright (c) 2023 Hamza Mahjoubi <[email protected]>
*
Expand Down Expand Up @@ -26772,7 +26753,7 @@ async function getSortingConfig() {
return { key, asc };
}
async function getViewConfigs() {
if (isPublic()) {
if (isPublicShare()) {
return null;
}
const url = _$2("apps/files/api/v1/views");
Expand Down Expand Up @@ -26999,20 +26980,20 @@ const genFileInfo = function(obj) {
return fileInfo;
};
function getDavPath({ filename, basename: basename3, source = "" }) {
if (isPublic()) {
if (isPublicShare()) {
return _$2(
`/s/${getToken()}/download?path={dirname}&files={basename}`,
`/s/${getSharingToken()}/download?path={dirname}&files={basename}`,
{ dirname: dirname2(filename), basename: basename3 }
);
}
const prefixUser = getUserRoot();
const prefixUser = davRootPath;
if (source && !source.includes(prefixUser)) {
return null;
}
if (filename.startsWith(prefixUser)) {
filename = filename.slice(prefixUser.length);
}
return getRootPath() + encodePath(filename);
return davRemoteURL + encodePath(filename);
}
/**
* @copyright Copyright (c) 2020 John Molakvoæ <[email protected]>
Expand Down Expand Up @@ -27253,21 +27234,7 @@ function pushToHistory({ fileid }) {
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
const getHeaders = () => {
return {
// Add this so the server knows it is an request from the browser
"X-Requested-With": "XMLHttpRequest",
// Add the request token to the request
requesttoken: d$3() || ""
};
};
const getClient = () => {
const client2 = an(
getRootPath(),
isPublic() ? { username: getToken(), password: "", headers: getHeaders() } : { headers: getHeaders() }
);
return client2;
};
const client = davGetClient();
/*! third party licenses: js/vendor.LICENSE.txt */
/**
* @copyright Copyright (c) 2019 John Molakvoæ <[email protected]>
Expand Down Expand Up @@ -27298,14 +27265,14 @@ const statData = `<?xml version="1.0"?>
</d:prop>
</d:propfind>`;
async function getFileInfo(path, options2 = {}) {
const response = await getClient().stat(path, Object.assign({
const response = await client.stat(`${davRootPath}${path}`, Object.assign({
data: statData,
details: true
}, options2));
return genFileInfo(response.data);
}
async function rawStat(origin2, path, options2 = {}) {
const response = await an(origin2, { headers: { requesttoken: d$3() || "" } }).stat(path, {
const response = await davGetClient(origin2).stat(path, {
...options2,
data: statData,
details: true
Expand Down Expand Up @@ -27335,8 +27302,7 @@ async function rawStat(origin2, path, options2 = {}) {
*
*/
async function getFileList(path, options2 = {}) {
const fixedPath = path === "/" ? "" : path;
const response = await getClient().getDirectoryContents(fixedPath, Object.assign({
const response = await client.getDirectoryContents(`${davRootPath}${path}`, Object.assign({
data: `<?xml version="1.0"?>
<d:propfind ${getDavNameSpaces()}>
<d:prop>
Expand Down Expand Up @@ -27448,8 +27414,8 @@ function getPreviewIfAny({ fileid, filename, previewUrl, hasPreview, davPath, et
}
const searchParams = `fileId=${fileid}&x=${Math.floor(screen.width * devicePixelRatio)}&y=${Math.floor(screen.height * devicePixelRatio)}&a=true` + (etag !== null ? `&etag=${etag.replace(/&quot;/g, "")}` : "");
if (hasPreview) {
if (isPublic()) {
return _$2(`/apps/files_sharing/publicpreview/${getToken()}?file=${encodePath(filename)}&${searchParams}`);
if (isPublicShare()) {
return _$2(`/apps/files_sharing/publicpreview/${getSharingToken()}?file=${encodePath(filename)}&${searchParams}`);
}
return _$2(`/core/preview?${searchParams}`);
}
Expand Down Expand Up @@ -28021,7 +27987,7 @@ const _sfc_main$D = {
// TODO: remove OCA?.Files?.fileActions when public Files is Vue
isStandalone: OCP?.Files === void 0 && OCA?.Files?.fileActions === void 0,
theme: null,
root: getRootPath(),
root: davRemoteURL,
handlerId: "",
trapElements: []
};
Expand Down Expand Up @@ -28086,7 +28052,7 @@ const _sfc_main$D = {
},
sidebarOpenFilePath() {
try {
const relativePath = this.currentFile?.davPath?.split(getUserRoot())[1];
const relativePath = this.currentFile?.davPath?.split(davRootPath)[1];
return relativePath?.split("/")?.map(decodeURIComponent)?.join("/");
} catch (e2) {
return false;
Expand Down Expand Up @@ -28756,7 +28722,7 @@ var __component__$D = /* @__PURE__ */ normalizeComponent$1(
_sfc_staticRenderFns$D,
false,
null,
"222ec41e"
"e16cf4dc"
);
const ViewerComponent = __component__$D.exports;
function setAsyncState(vm, stateObject, state) {
Expand Down
2 changes: 1 addition & 1 deletion js/viewer-main.mjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"@nextcloud/browserslist-config": "^3.0.1",
"@nextcloud/cypress": "^1.0.0-beta.8",
"@nextcloud/eslint-config": "^8.4.1",
"@nextcloud/sharing": "^0.2.3",
"@nextcloud/stylelint-config": "^3.0.1",
"@nextcloud/vite-config": "^1.4.0",
"@types/dockerode": "^3.3.29",
Expand Down
12 changes: 6 additions & 6 deletions src/services/FileInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
*
*/

import { getClient } from './WebdavClient'
import type { FileStat, ResponseDataDetailed } from 'webdav'

import { client } from './WebdavClient'
import { genFileInfo, type FileInfo } from '../utils/fileUtils'
import { createClient, type FileStat, type ResponseDataDetailed } from 'webdav'
import { getRequestToken } from '@nextcloud/auth'
import { getDavNameSpaces, getDavProperties } from '@nextcloud/files'
import { davGetClient, davRootPath, getDavNameSpaces, getDavProperties } from '@nextcloud/files'

const statData = `<?xml version="1.0"?>
<d:propfind ${getDavNameSpaces()}>
Expand All @@ -40,7 +40,7 @@ const statData = `<?xml version="1.0"?>
* @param options
*/
export default async function(path: string, options = {}): Promise<FileInfo> {
const response = await getClient().stat(path, Object.assign({
const response = await client.stat(`${davRootPath}${path}`, Object.assign({
data: statData,
details: true,
}, options)) as ResponseDataDetailed<FileStat>
Expand All @@ -54,7 +54,7 @@ export default async function(path: string, options = {}): Promise<FileInfo> {
* @param options
*/
export async function rawStat(origin: string, path: string, options = {}) {
const response = await createClient(origin, { headers: { requesttoken: getRequestToken() || '' } }).stat(path, {
const response = await davGetClient(origin).stat(path, {
...options,
data: statData,
details: true,
Expand Down
9 changes: 3 additions & 6 deletions src/services/FileList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*
*/

import { getDavNameSpaces, getDavProperties } from '@nextcloud/files'
import { getClient } from './WebdavClient'
import { davRootPath, getDavNameSpaces, getDavProperties } from '@nextcloud/files'
import { client } from './WebdavClient'
import { genFileInfo, type FileInfo } from '../utils/fileUtils'
import type { FileStat, ResponseDataDetailed } from 'webdav'

Expand All @@ -31,10 +31,7 @@ import type { FileStat, ResponseDataDetailed } from 'webdav'
* @param options
*/
export default async function(path: string, options = {}): Promise<FileInfo[]> {
// getDirectoryContents doesn't accept / for root
const fixedPath = path === '/' ? '' : path

const response = await getClient().getDirectoryContents(fixedPath, Object.assign({
const response = await client.getDirectoryContents(`${davRootPath}${path}`, Object.assign({
data: `<?xml version="1.0"?>
<d:propfind ${getDavNameSpaces()}>
<d:prop>
Expand Down
4 changes: 2 additions & 2 deletions src/services/FileSortingConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*/

import axios from '@nextcloud/axios'
import { isPublic } from '../utils/davUtils'
import { generateUrl } from '@nextcloud/router'
import { isPublicShare } from '@nextcloud/sharing/public'

/**
* @return {object}
Expand All @@ -45,7 +45,7 @@ export default async function getSortingConfig() {
* @return {object}
*/
async function getViewConfigs() {
if (isPublic()) {
if (isPublicShare()) {
return null
}
const url = generateUrl('apps/files/api/v1/views')
Expand Down
23 changes: 2 additions & 21 deletions src/services/WebdavClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,6 @@
*
*/

import { createClient } from 'webdav'
import { getRootPath, getToken, isPublic } from '../utils/davUtils'
import { getRequestToken } from '@nextcloud/auth'
import { davGetClient } from '@nextcloud/files'

// Use a method for the headers, to always get the current request token
const getHeaders = () => {
return {
// Add this so the server knows it is an request from the browser
'X-Requested-With': 'XMLHttpRequest',
// Add the request token to the request
requesttoken: getRequestToken() || '',
}
}

export const getClient = () => {
const client = createClient(getRootPath(), isPublic()
? { username: getToken(), password: '', headers: getHeaders() }
: { headers: getHeaders() },
)

return client
}
export const client = davGetClient()
Loading

0 comments on commit 7417482

Please sign in to comment.