mirror of
https://github.com/bpatrik/pigallery2.git
synced 2025-01-04 03:49:28 +02:00
implementing share management backend features #145
This commit is contained in:
parent
b9f5aed01c
commit
ae447878d0
@ -5,6 +5,7 @@ import {Config, PrivateConfigClass} from '../../common/config/private/Config';
|
|||||||
import {UserDTO, UserRoles} from '../../common/entities/UserDTO';
|
import {UserDTO, UserRoles} from '../../common/entities/UserDTO';
|
||||||
import {NotificationManager} from '../model/NotifocationManager';
|
import {NotificationManager} from '../model/NotifocationManager';
|
||||||
import {Logger} from '../Logger';
|
import {Logger} from '../Logger';
|
||||||
|
import {SharingDTO} from '../../common/entities/SharingDTO';
|
||||||
|
|
||||||
export class RenderingMWs {
|
export class RenderingMWs {
|
||||||
|
|
||||||
@ -47,6 +48,16 @@ export class RenderingMWs {
|
|||||||
RenderingMWs.renderMessage(res, sharing);
|
RenderingMWs.renderMessage(res, sharing);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static renderSharingList(req: Request, res: Response, next: NextFunction) {
|
||||||
|
if (!req.resultPipe) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
req.resultPipe.forEach((s: SharingDTO) => delete s.password);
|
||||||
|
return RenderingMWs.renderMessage(res, req.resultPipe);
|
||||||
|
}
|
||||||
|
|
||||||
public static renderFile(req: Request, res: Response, next: NextFunction) {
|
public static renderFile(req: Request, res: Response, next: NextFunction) {
|
||||||
if (!req.resultPipe) {
|
if (!req.resultPipe) {
|
||||||
return next();
|
return next();
|
||||||
|
@ -5,6 +5,7 @@ import {ErrorCodes, ErrorDTO} from '../../common/entities/Error';
|
|||||||
import {Config} from '../../common/config/private/Config';
|
import {Config} from '../../common/config/private/Config';
|
||||||
import {QueryParams} from '../../common/QueryParams';
|
import {QueryParams} from '../../common/QueryParams';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
import {UserRoles} from '../../common/entities/UserDTO';
|
||||||
|
|
||||||
|
|
||||||
export class SharingMWs {
|
export class SharingMWs {
|
||||||
@ -91,7 +92,8 @@ export class SharingMWs {
|
|||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
req.resultPipe = await ObjectManagers.getInstance().SharingManager.updateSharing(sharing);
|
const forceUpdate = req.session.user.role >= UserRoles.Admin;
|
||||||
|
req.resultPipe = await ObjectManagers.getInstance().SharingManager.updateSharing(sharing, forceUpdate);
|
||||||
return next();
|
return next();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, 'Error during updating sharing link', err));
|
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, 'Error during updating sharing link', err));
|
||||||
@ -99,6 +101,37 @@ export class SharingMWs {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static async deleteSharing(req: Request, res: Response, next: NextFunction) {
|
||||||
|
if (Config.Client.Sharing.enabled === false) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
if ((typeof req.params === 'undefined') || (typeof req.params.sharingKey === 'undefined')) {
|
||||||
|
return next(new ErrorDTO(ErrorCodes.INPUT_ERROR, 'sharingKey is missing'));
|
||||||
|
}
|
||||||
|
const sharingKey: string = req.params.sharingKey;
|
||||||
|
|
||||||
|
try {
|
||||||
|
req.resultPipe = await ObjectManagers.getInstance().SharingManager.deleteSharing(sharingKey);
|
||||||
|
return next();
|
||||||
|
} catch (err) {
|
||||||
|
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, 'Error during deleting sharing', err));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async listSharing(req: Request, res: Response, next: NextFunction) {
|
||||||
|
if (Config.Client.Sharing.enabled === false) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
req.resultPipe = await ObjectManagers.getInstance().SharingManager.find({});
|
||||||
|
return next();
|
||||||
|
} catch (err) {
|
||||||
|
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, 'Error during listing shares', err));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static generateKey(): string {
|
private static generateKey(): string {
|
||||||
function s4() {
|
function s4() {
|
||||||
return Math.floor((1 + Math.random()) * 0x10000)
|
return Math.floor((1 + Math.random()) * 0x10000)
|
||||||
|
@ -5,5 +5,9 @@ export interface ISharingManager {
|
|||||||
|
|
||||||
createSharing(sharing: SharingDTO): Promise<SharingDTO>;
|
createSharing(sharing: SharingDTO): Promise<SharingDTO>;
|
||||||
|
|
||||||
updateSharing(sharing: SharingDTO): Promise<SharingDTO>;
|
updateSharing(sharing: SharingDTO, forceUpdate: boolean): Promise<SharingDTO>;
|
||||||
|
|
||||||
|
find(filter: any): Promise<SharingDTO[]>;
|
||||||
|
|
||||||
|
deleteSharing(sharingKey: string): Promise<void>;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,13 @@ import {SharingDTO} from '../../../../common/entities/SharingDTO';
|
|||||||
|
|
||||||
export class SharingManager implements ISharingManager {
|
export class SharingManager implements ISharingManager {
|
||||||
|
|
||||||
|
deleteSharing(sharingKey: string): Promise<void> {
|
||||||
|
throw new Error('not implemented');
|
||||||
|
}
|
||||||
|
|
||||||
|
find(filter: any): Promise<SharingDTO[]> {
|
||||||
|
throw new Error('not implemented');
|
||||||
|
}
|
||||||
|
|
||||||
findOne(filter: any): Promise<SharingDTO> {
|
findOne(filter: any): Promise<SharingDTO> {
|
||||||
throw new Error('not implemented');
|
throw new Error('not implemented');
|
||||||
@ -12,7 +19,7 @@ export class SharingManager implements ISharingManager {
|
|||||||
throw new Error('not implemented');
|
throw new Error('not implemented');
|
||||||
}
|
}
|
||||||
|
|
||||||
updateSharing(sharing: SharingDTO): Promise<SharingDTO> {
|
updateSharing(sharing: SharingDTO, forceUpdate: boolean): Promise<SharingDTO> {
|
||||||
throw new Error('not implemented');
|
throw new Error('not implemented');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import {SharingEntity} from './enitites/SharingEntity';
|
|||||||
import {Config} from '../../../../common/config/private/Config';
|
import {Config} from '../../../../common/config/private/Config';
|
||||||
import {PasswordHelper} from '../../PasswordHelper';
|
import {PasswordHelper} from '../../PasswordHelper';
|
||||||
import {DeleteResult} from 'typeorm';
|
import {DeleteResult} from 'typeorm';
|
||||||
|
import {UserEntity} from './enitites/UserEntity';
|
||||||
|
|
||||||
export class SharingManager implements ISharingManager {
|
export class SharingManager implements ISharingManager {
|
||||||
|
|
||||||
@ -18,6 +19,18 @@ export class SharingManager implements ISharingManager {
|
|||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async deleteSharing(sharingKey: string): Promise<void> {
|
||||||
|
const connection = await SQLConnection.getConnection();
|
||||||
|
const sharing = await connection.getRepository(SharingEntity).findOne({sharingKey: sharingKey});
|
||||||
|
await connection.getRepository(SharingEntity).remove(sharing);
|
||||||
|
}
|
||||||
|
|
||||||
|
async find(filter: any): Promise<SharingDTO[]> {
|
||||||
|
await SharingManager.removeExpiredLink();
|
||||||
|
const connection = await SQLConnection.getConnection();
|
||||||
|
return await connection.getRepository(SharingEntity).find(filter);
|
||||||
|
}
|
||||||
|
|
||||||
async findOne(filter: any): Promise<SharingDTO> {
|
async findOne(filter: any): Promise<SharingDTO> {
|
||||||
await SharingManager.removeExpiredLink();
|
await SharingManager.removeExpiredLink();
|
||||||
const connection = await SQLConnection.getConnection();
|
const connection = await SQLConnection.getConnection();
|
||||||
@ -33,7 +46,7 @@ export class SharingManager implements ISharingManager {
|
|||||||
return connection.getRepository(SharingEntity).save(sharing);
|
return connection.getRepository(SharingEntity).save(sharing);
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateSharing(inSharing: SharingDTO): Promise<SharingDTO> {
|
async updateSharing(inSharing: SharingDTO, forceUpdate: boolean): Promise<SharingDTO> {
|
||||||
const connection = await SQLConnection.getConnection();
|
const connection = await SQLConnection.getConnection();
|
||||||
|
|
||||||
const sharing = await connection.getRepository(SharingEntity).findOne({
|
const sharing = await connection.getRepository(SharingEntity).findOne({
|
||||||
@ -42,7 +55,7 @@ export class SharingManager implements ISharingManager {
|
|||||||
path: inSharing.path
|
path: inSharing.path
|
||||||
});
|
});
|
||||||
|
|
||||||
if (sharing.timeStamp < Date.now() - Config.Server.Sharing.updateTimeout) {
|
if (sharing.timeStamp < Date.now() - Config.Server.Sharing.updateTimeout && forceUpdate !== true) {
|
||||||
throw new Error('Sharing is locked, can\'t update anymore');
|
throw new Error('Sharing is locked, can\'t update anymore');
|
||||||
}
|
}
|
||||||
if (inSharing.password == null) {
|
if (inSharing.password == null) {
|
||||||
|
@ -12,6 +12,7 @@ export class SharingRouter {
|
|||||||
this.addGetSharing(app);
|
this.addGetSharing(app);
|
||||||
this.addCreateSharing(app);
|
this.addCreateSharing(app);
|
||||||
this.addUpdateSharing(app);
|
this.addUpdateSharing(app);
|
||||||
|
this.addListSharing(app);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static addShareLogin(app: express.Express) {
|
private static addShareLogin(app: express.Express) {
|
||||||
@ -49,4 +50,23 @@ export class SharingRouter {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static addDeleteSharing(app: express.Express) {
|
||||||
|
app.delete(['/api/share/:sharingKey'],
|
||||||
|
AuthenticationMWs.authenticate,
|
||||||
|
AuthenticationMWs.authorise(UserRoles.Admin),
|
||||||
|
SharingMWs.updateSharing,
|
||||||
|
RenderingMWs.renderOK
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static addListSharing(app: express.Express) {
|
||||||
|
app.get(['/api/share/list'],
|
||||||
|
AuthenticationMWs.authenticate,
|
||||||
|
AuthenticationMWs.authorise(UserRoles.User),
|
||||||
|
SharingMWs.listSharing,
|
||||||
|
RenderingMWs.renderSharingList
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -121,7 +121,7 @@ describe('SharingManager', (sqlHelper: SQLTestHelper) => {
|
|||||||
includeSubfolders: false,
|
includeSubfolders: false,
|
||||||
timeStamp: Date.now()
|
timeStamp: Date.now()
|
||||||
};
|
};
|
||||||
const updated = await sm.updateSharing(update);
|
const updated = await sm.updateSharing(update, false);
|
||||||
|
|
||||||
expect(updated.id).to.equals(saved.id);
|
expect(updated.id).to.equals(saved.id);
|
||||||
expect(updated.sharingKey).to.equals(sharing.sharingKey);
|
expect(updated.sharingKey).to.equals(sharing.sharingKey);
|
||||||
|
Loading…
Reference in New Issue
Block a user