[feature-107] progress on playlists (lot of things, i don't remember all things i've done in this commit)

This commit is contained in:
MathieuG-P
2024-03-12 06:49:32 +01:00
parent 501def3a94
commit 7237e49c7f
29 changed files with 316 additions and 139 deletions
@@ -11,13 +11,12 @@ import { BPList, DownloadPlaylistProgressionData } from "shared/models/playlists
import { readFileSync } from "fs";
import { BeatSaverService } from "../thrid-party/beat-saver/beat-saver.service";
import { copy, copyFile, ensureDir, pathExists, pathExistsSync, readdirSync, realpath } from "fs-extra";
import { Progression, pathExist } from "../../helpers/fs.helpers";
import { Progression, pathExist, unlinkPath } from "../../helpers/fs.helpers";
import { FileAssociationService } from "../file-association.service";
import { SongDetailsCacheService } from "./maps/song-details-cache.service";
import { sToMs } from "shared/helpers/time.helpers";
import { LocalBPList, LocalBPListsDetails } from "shared/models/playlists/local-playlist.models";
import { SongCacheService } from "./maps/song-cache.service";
import { pathToFileURL } from "url";
import { InstallationLocationService } from "../installation-location.service";
export class LocalPlaylistsManagerService {
@@ -229,6 +228,40 @@ export class LocalPlaylistsManagerService {
}
public deletePlaylist(opt: {path: string, deleteMaps?: boolean}): Observable<Progression>{
console.log("AAAAAA");
return new Observable<Progression>(obs => {
(async () => {
const bpList = await this.readPlaylistFile(opt.path);
const progress: Progression = { current: 0, total: opt.deleteMaps ? bpList.songs.length + 1 : 1};
if(opt.deleteMaps){
const mapsHashs = bpList.songs.map(s => ({ hash: s.hash }));
await lastValueFrom(this.maps.deleteMaps(mapsHashs).pipe(
tap({
next: () => {
progress.current += 1
obs.next(progress);
},
error: err => obs.error(err),
complete: () => obs.next(progress),
}),
));
}
await unlinkPath(opt.path);
progress.current += 1;
obs.next(progress);
})()
.catch(err => obs.error(err))
.finally(() => obs.complete());
});
}
public oneClickInstallPlaylist(bpListUrl: string): Observable<Progression<DownloadPlaylistProgressionData>> {
return new Observable<Progression<DownloadPlaylistProgressionData>>(obs => {
@@ -7,7 +7,7 @@ import { InstallationLocationService } from "../../installation-location.service
import { UtilsService } from "../../utils.service";
import crypto from "crypto";
import { lstatSync } from "fs";
import { copy, createReadStream, ensureDir, pathExists, realpath, unlink } from "fs-extra";
import { copy, createReadStream, ensureDir, pathExists, pathExistsSync, realpath, unlink } from "fs-extra";
import StreamZip from "node-stream-zip";
import { RequestService } from "../../request.service";
import sanitize from "sanitize-filename";
@@ -240,32 +240,33 @@ export class LocalMapsManagerService {
return this.linker.unlinkFolder(versionMapsPath, { keepContents: keepMaps, intermediateFolder: LocalMapsManagerService.SHARED_MAPS_FOLDER });
}
public deleteMaps(maps: BsmLocalMap[]): Observable<DeleteMapsProgress> {
const mapsFolders = maps.map(map => map.path);
const mapsHashsToDelete = maps.map(map => map.hash);
public deleteMaps(maps: Partial<BsmLocalMap>[]): Observable<DeleteMapsProgress> {
return new Observable<DeleteMapsProgress>(observer => {
const progress: DeleteMapsProgress = { total: maps.length, deleted: 0 };
(async () => {
const progress: DeleteMapsProgress = { total: maps.length, deleted: 0 };
try {
for (const folder of mapsFolders) {
const detail = await this.loadMapInfoFromPath(folder);
if (!mapsHashsToDelete.includes(detail?.hash)) {
continue;
}
await deleteFolder(folder);
this.songCache.deleteMapInfoFromDirname(path.basename(folder));
for (const map of maps) {
let mapPath = map.path;
if (!mapPath) {
const mapInfo = map.hash ? this.songCache.getMapInfoFromHash(map.hash) : null;
mapPath = mapInfo?.path;
}
if (mapPath && pathExistsSync(mapPath)) {
await deleteFolder(mapPath);
this.songCache.deleteMapInfoFromDirname(path.basename(mapPath));
progress.deleted++;
observer.next(progress);
}
} catch (e) {
observer.error(e);
}
observer.complete();
})();
})()
.catch(e => observer.error(e))
.finally(() => observer.complete());
});
}
public async downloadMap(map: BsvMapDetail, version?: BSVersion): Promise<BsmLocalMap> {
if (!map.versions.at(0).hash) {
throw new Error("Cannot download map, no hash found");
+4 -1
View File
@@ -54,10 +54,13 @@ export class IpcService {
complete: () => this.send(this.getCompleteChannel(channel), window)
})
window.webContents.once("destroyed", () => sub.unsubscribe());
const unsubscribeOnDestroy = () => sub.unsubscribe();
window.webContents.once("destroyed", unsubscribeOnDestroy);
window.webContents.ipc.once(this.getTearDownChannel(channel), () => sub.unsubscribe());
sub.add(() => {
window.webContents.removeListener("destroyed", unsubscribeOnDestroy);
window.webContents.ipc.removeAllListeners(this.getTearDownChannel(channel));
});
}