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

[FIX] "Save to WebDav" not working #18883

Merged
merged 3 commits into from
Sep 16, 2020
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { createClient } from 'webdav';

import type { WebDavClient, Stat } from '../../../../definition/webdav';

export type ServerCredentials = {
token?: string;
username?: string;
password?: string;
};

export class WebdavClientAdapter {
constructor(serverConfig, cred) {
_client: WebDavClient;

constructor(serverConfig: string, cred: ServerCredentials) {
if (cred.token) {
this._client = createClient(
serverConfig,
Expand All @@ -18,51 +28,59 @@ export class WebdavClientAdapter {
}
}

async stat(path) {
async stat(path: string): Promise<undefined> {
try {
return await this._client.stat(path);
} catch (error) {
throw new Error(error.response && error.response.statusText ? error.response.statusText : 'Error checking if directory exists on webdav');
}
}

async createDirectory(path) {
async createDirectory(path: string): Promise<Response> {
try {
return await this._client.createDirectory(path);
} catch (error) {
throw new Error(error.response && error.response.statusText ? error.response.statusText : 'Error creating directory on webdav');
}
}

async deleteFile(path) {
async deleteFile(path: string): Promise<Response> {
try {
return await this._client.deleteFile(path);
} catch (error) {
throw new Error(error.response && error.response.statusText ? error.response.statusText : 'Error deleting file on webdav');
}
}

async getFileContents(filename) {
async getFileContents(filename: string): Promise<Buffer> {
try {
return await this._client.getFileContents(filename);
return await this._client.getFileContents(filename) as Buffer;
} catch (error) {
throw new Error(error.response && error.response.statusText ? error.response.statusText : 'Error getting file contents webdav');
}
}

async getDirectoryContents(path) {
async getDirectoryContents(path: string): Promise<Array<Stat>> {
try {
return await this._client.getDirectoryContents(path);
} catch (error) {
throw new Error(error.response && error.response.statusText ? error.response.statusText : 'Error getting directory contents webdav');
}
}

createReadStream(path, options) {
async putFileContents(path: string, data: Buffer, options: Record<string, any> = {}): Promise<any> {
try {
return await this._client.putFileContents(path, data, options);
} catch (error) {
throw new Error(error.response?.statusText ?? 'Error updating file contents.');
}
}

createReadStream(path: string, options?: Record<string, any>): ReadableStream {
return this._client.createReadStream(path, options);
}

createWriteStream(path) {
createWriteStream(path: string): WritableStream {
return this._client.createWriteStream(path);
}
}
7 changes: 0 additions & 7 deletions app/webdav/server/methods/getWebdavCredentials.js

This file was deleted.

9 changes: 9 additions & 0 deletions app/webdav/server/methods/getWebdavCredentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ServerCredentials } from '../lib/webdavClientAdapter';

export function getWebdavCredentials(account: ServerCredentials): ServerCredentials {
const cred = account.token ? { token: account.token } : {
username: account.username,
password: account.password,
};
return cred;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Meteor } from 'meteor/meteor';

import { settings } from '../../../settings';
import { Logger } from '../../../logger';
import { settings } from '../../../settings/server';
import { Logger } from '../../../logger/server';
import { getWebdavCredentials } from './getWebdavCredentials';
import { WebdavAccounts } from '../../../models';
import { WebdavAccounts } from '../../../models/server';
import { WebdavClientAdapter } from '../lib/webdavClientAdapter';

const logger = new Logger('WebDAV_Upload', {});
Expand All @@ -29,11 +29,14 @@ Meteor.methods({
try {
const cred = getWebdavCredentials(account);
const client = new WebdavClientAdapter(account.server_url, cred);
// eslint-disable-next-line @typescript-eslint/no-empty-function
await client.createDirectory(uploadFolder).catch(() => {});
await client.putFileContents(`${ uploadFolder }/${ name }`, buffer, { overwrite: false });
return { success: true };
} catch (error) {
// @ts-ignore
logger.error(error);

if (error.response) {
const { status } = error.response;
if (status === 404) {
Expand Down
32 changes: 32 additions & 0 deletions definition/webdav.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export type Stat = {
filename: string;
basename: string;
lastmod: string|null;
size: number;
type: string;
mime: string;
etag: string|null;
props: Record<string, any>;
}

export type WebDavClient = {
copyFile(remotePath: string, targetRemotePath: string, options?: Record<string, any>): Promise<any>;
createDirectory(dirPath: string, options?: Record<string, any>): Promise<Response>;
createReadStream(remoteFileName: string, options?: Record<string, any>): ReadableStream;
createWriteStream(remoteFileName: string, options?: Record<string, any>, callback?: Function): WritableStream;
customRequest(remotePath: string, requestOptions: Record<string, any>, options?: Record<string, any>): Promise<any>;
deleteFile(remotePath: string, options?: Record<string, any>): Promise<Response>;
exists(remotePath: string, options?: Record<string, any>): Promise<boolean>;
getDirectoryContents(remotePath: string, options?: Record<string, any>): Promise<Array<Stat>>;
getFileContents(remoteFileName: string, options?: Record<string, any>): Promise<Buffer|string>;
getFileDownloadLink(remoteFileName: string, options?: Record<string, any>): string;
getFileUploadLink(remoteFileName: string, options?: Record<string, any>): string;
getQuota(options?: Record<string, any>): Promise<null|object>;
moveFile(remotePath: string, targetRemotePath: string, options?: Record<string, any>): Promise<any>;
putFileContents(remoteFileName: string, data: string|Buffer, options?: Record<string, any>): Promise<any>;
stat(remotePath: string, options?: Record<string, any>): Promise<any>;
}

declare module 'webdav' {
export function createClient(remoteURL: string, opts?: Record<string, any>): WebDavClient;
}
2 changes: 1 addition & 1 deletion server/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ declare module 'meteor/meteor' {
interface Error extends globalError {
error: string | number;
reason?: string;
details?: string | undefined;
details?: string | undefined | Record<string, string>;
}

const server: any;
Expand Down
1 change: 1 addition & 0 deletions typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ declare module 'meteor/promise';
declare module 'meteor/ddp-common';
declare module 'meteor/routepolicy';
declare module 'xml-encryption';
declare module 'webdav';