2018-03-31 03:30:30 +08:00
|
|
|
import {ISharingManager} from '../interfaces/ISharingManager';
|
|
|
|
import {SharingDTO} from '../../../common/entities/SharingDTO';
|
|
|
|
import {SQLConnection} from './SQLConnection';
|
|
|
|
import {SharingEntity} from './enitites/SharingEntity';
|
|
|
|
import {Config} from '../../../common/config/private/Config';
|
|
|
|
import {PasswordHelper} from '../PasswordHelper';
|
2017-07-04 01:17:49 +08:00
|
|
|
|
|
|
|
export class SharingManager implements ISharingManager {
|
|
|
|
|
2018-02-04 04:07:50 +08:00
|
|
|
private static async removeExpiredLink() {
|
|
|
|
const connection = await SQLConnection.getConnection();
|
|
|
|
return connection
|
|
|
|
.getRepository(SharingEntity)
|
2018-03-31 03:30:30 +08:00
|
|
|
.createQueryBuilder('share')
|
|
|
|
.where('expires < :now', {now: Date.now()})
|
2018-02-04 04:07:50 +08:00
|
|
|
.delete()
|
|
|
|
.execute();
|
|
|
|
}
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
async findOne(filter: any): Promise<SharingDTO> {
|
2018-02-04 04:07:50 +08:00
|
|
|
await SharingManager.removeExpiredLink();
|
2017-07-21 05:37:10 +08:00
|
|
|
const connection = await SQLConnection.getConnection();
|
2017-07-04 01:17:49 +08:00
|
|
|
return await connection.getRepository(SharingEntity).findOne(filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
async createSharing(sharing: SharingDTO): Promise<SharingDTO> {
|
2018-02-04 04:07:50 +08:00
|
|
|
await SharingManager.removeExpiredLink();
|
2017-07-21 05:37:10 +08:00
|
|
|
const connection = await SQLConnection.getConnection();
|
2017-07-10 04:36:25 +08:00
|
|
|
if (sharing.password) {
|
2017-07-11 15:01:59 +08:00
|
|
|
sharing.password = PasswordHelper.cryptPassword(sharing.password);
|
2017-07-10 04:36:25 +08:00
|
|
|
}
|
2017-10-20 00:08:07 +08:00
|
|
|
return await connection.getRepository(SharingEntity).save(sharing);
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async updateSharing(inSharing: SharingDTO): Promise<SharingDTO> {
|
2017-07-21 05:37:10 +08:00
|
|
|
const connection = await SQLConnection.getConnection();
|
2017-07-04 01:17:49 +08:00
|
|
|
|
2018-02-04 04:07:50 +08:00
|
|
|
const sharing = await connection.getRepository(SharingEntity).findOne({
|
2017-07-04 01:17:49 +08:00
|
|
|
id: inSharing.id,
|
2018-02-04 04:07:50 +08:00
|
|
|
creator: <any>inSharing.creator.id,
|
2017-07-04 01:17:49 +08:00
|
|
|
path: inSharing.path
|
|
|
|
});
|
|
|
|
|
|
|
|
if (sharing.timeStamp < Date.now() - Config.Server.sharing.updateTimeout) {
|
2018-05-13 00:19:51 +08:00
|
|
|
throw new Error('Sharing is locked, can\'t update anymore');
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
2018-02-04 09:50:05 +08:00
|
|
|
if (inSharing.password == null) {
|
|
|
|
sharing.password = null;
|
|
|
|
} else {
|
|
|
|
sharing.password = PasswordHelper.cryptPassword(inSharing.password);
|
|
|
|
}
|
2017-07-04 01:17:49 +08:00
|
|
|
sharing.includeSubfolders = inSharing.includeSubfolders;
|
|
|
|
sharing.expires = inSharing.expires;
|
|
|
|
|
2017-10-20 00:08:07 +08:00
|
|
|
return await connection.getRepository(SharingEntity).save(sharing);
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|