You've already forked pigallery2
mirror of
https://github.com/bpatrik/pigallery2.git
synced 2025-07-13 01:20:23 +02:00
adding sqlite support, removing mysql required dependency
This commit is contained in:
58
backend/model/sql/SharingManager.ts
Normal file
58
backend/model/sql/SharingManager.ts
Normal file
@ -0,0 +1,58 @@
|
||||
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";
|
||||
|
||||
export class SharingManager implements ISharingManager {
|
||||
|
||||
async findOne(filter: any): Promise<SharingDTO> {
|
||||
await this.removeExpiredLink();
|
||||
const connection = await SQLConnection.getConnection();
|
||||
return await connection.getRepository(SharingEntity).findOne(filter);
|
||||
}
|
||||
|
||||
async createSharing(sharing: SharingDTO): Promise<SharingDTO> {
|
||||
await this.removeExpiredLink();
|
||||
const connection = await SQLConnection.getConnection();
|
||||
if (sharing.password) {
|
||||
sharing.password = PasswordHelper.cryptPassword(sharing.password);
|
||||
}
|
||||
return await connection.getRepository(SharingEntity).persist(sharing);
|
||||
|
||||
|
||||
}
|
||||
|
||||
async updateSharing(inSharing: SharingDTO): Promise<SharingDTO> {
|
||||
const connection = await SQLConnection.getConnection();
|
||||
|
||||
let sharing = await connection.getRepository(SharingEntity).findOne({
|
||||
id: inSharing.id,
|
||||
creator: inSharing.creator.id,
|
||||
path: inSharing.path
|
||||
});
|
||||
|
||||
if (sharing.timeStamp < Date.now() - Config.Server.sharing.updateTimeout) {
|
||||
throw "Sharing is locked, can't update anymore"
|
||||
}
|
||||
|
||||
sharing.password = inSharing.password;
|
||||
sharing.includeSubfolders = inSharing.includeSubfolders;
|
||||
sharing.expires = inSharing.expires;
|
||||
|
||||
return await connection.getRepository(SharingEntity).persist(sharing);
|
||||
}
|
||||
|
||||
private async removeExpiredLink() {
|
||||
const connection = await SQLConnection.getConnection();
|
||||
return connection
|
||||
.getRepository(SharingEntity)
|
||||
.createQueryBuilder("share")
|
||||
.where("expires < :now", {now: Date.now()})
|
||||
.delete()
|
||||
.execute();
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user