2018-03-31 03:30:30 +08:00
|
|
|
import {NextFunction, Request, Response} from 'express';
|
|
|
|
import {CreateSharingDTO, SharingDTO} from '../../common/entities/SharingDTO';
|
|
|
|
import {ObjectManagerRepository} from '../model/ObjectManagerRepository';
|
|
|
|
import {ErrorCodes, ErrorDTO} from '../../common/entities/Error';
|
2018-10-22 06:24:17 +08:00
|
|
|
import {Config} from '../../common/config/private/Config';
|
2018-03-31 03:30:30 +08:00
|
|
|
|
|
|
|
const LOG_TAG = '[SharingMWs]';
|
2017-07-04 01:17:49 +08:00
|
|
|
|
|
|
|
export class SharingMWs {
|
|
|
|
|
|
|
|
|
|
|
|
private static generateKey(): string {
|
|
|
|
function s4() {
|
|
|
|
return Math.floor((1 + Math.random()) * 0x10000)
|
|
|
|
.toString(16)
|
|
|
|
.substring(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return s4() + s4();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static async getSharing(req: Request, res: Response, next: NextFunction) {
|
2018-10-22 06:24:17 +08:00
|
|
|
if (Config.Client.Sharing.enabled === false) {
|
|
|
|
return next();
|
|
|
|
}
|
2017-07-04 01:17:49 +08:00
|
|
|
const sharingKey = req.params.sharingKey;
|
|
|
|
|
|
|
|
try {
|
|
|
|
req.resultPipe = await ObjectManagerRepository.getInstance().SharingManager.findOne({sharingKey: sharingKey});
|
|
|
|
return next();
|
|
|
|
|
|
|
|
} catch (err) {
|
2018-03-31 03:30:30 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, 'Error during retrieving sharing link', err));
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async createSharing(req: Request, res: Response, next: NextFunction) {
|
2018-10-22 06:24:17 +08:00
|
|
|
if (Config.Client.Sharing.enabled === false) {
|
|
|
|
return next();
|
|
|
|
}
|
2017-07-04 01:17:49 +08:00
|
|
|
if ((typeof req.body === 'undefined') || (typeof req.body.createSharing === 'undefined')) {
|
2018-03-31 03:30:30 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.INPUT_ERROR, 'createSharing filed is missing'));
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
|
|
|
const createSharing: CreateSharingDTO = req.body.createSharing;
|
|
|
|
let sharingKey = SharingMWs.generateKey();
|
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
// create one not yet used
|
2017-07-04 01:17:49 +08:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
await ObjectManagerRepository.getInstance().SharingManager.findOne({sharingKey: sharingKey});
|
|
|
|
sharingKey = this.generateKey();
|
|
|
|
} catch (err) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
const directoryName = req.params.directory || '/';
|
2018-05-13 00:19:51 +08:00
|
|
|
const sharing: SharingDTO = {
|
2017-07-04 01:17:49 +08:00
|
|
|
id: null,
|
|
|
|
sharingKey: sharingKey,
|
|
|
|
path: directoryName,
|
|
|
|
password: createSharing.password,
|
|
|
|
creator: req.session.user,
|
|
|
|
expires: Date.now() + createSharing.valid,
|
|
|
|
includeSubfolders: createSharing.includeSubfolders,
|
|
|
|
timeStamp: Date.now()
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
req.resultPipe = await ObjectManagerRepository.getInstance().SharingManager.createSharing(sharing);
|
|
|
|
return next();
|
|
|
|
|
|
|
|
} catch (err) {
|
2018-05-24 09:53:53 +08:00
|
|
|
console.warn(err);
|
2018-03-31 03:30:30 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, 'Error during creating sharing link', err));
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async updateSharing(req: Request, res: Response, next: NextFunction) {
|
2018-10-22 06:24:17 +08:00
|
|
|
if (Config.Client.Sharing.enabled === false) {
|
|
|
|
return next();
|
|
|
|
}
|
2017-07-04 01:17:49 +08:00
|
|
|
if ((typeof req.body === 'undefined') || (typeof req.body.updateSharing === 'undefined')) {
|
2018-03-31 03:30:30 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.INPUT_ERROR, 'updateSharing filed is missing'));
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
|
|
|
const updateSharing: CreateSharingDTO = req.body.updateSharing;
|
2018-03-31 03:30:30 +08:00
|
|
|
const directoryName = req.params.directory || '/';
|
2018-05-13 00:19:51 +08:00
|
|
|
const sharing: SharingDTO = {
|
2017-07-04 01:17:49 +08:00
|
|
|
id: updateSharing.id,
|
|
|
|
path: directoryName,
|
2018-03-31 03:30:30 +08:00
|
|
|
sharingKey: '',
|
2018-05-17 05:47:32 +08:00
|
|
|
password: (updateSharing.password && updateSharing.password !== '') ? updateSharing.password : null,
|
2017-07-04 01:17:49 +08:00
|
|
|
creator: req.session.user,
|
|
|
|
expires: Date.now() + updateSharing.valid,
|
|
|
|
includeSubfolders: updateSharing.includeSubfolders,
|
|
|
|
timeStamp: Date.now()
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
req.resultPipe = await ObjectManagerRepository.getInstance().SharingManager.updateSharing(sharing);
|
|
|
|
return next();
|
|
|
|
} catch (err) {
|
2018-03-31 03:30:30 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, 'Error during updating sharing link', err));
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|