Fix recording sync (#291)

* delete audio/video/recording in remote

* sync recordings on profile page

* handle recording sync failed
This commit is contained in:
an-lee
2024-02-09 18:24:36 +08:00
committed by GitHub
parent 27c342cabb
commit 338ef82a1e
10 changed files with 115 additions and 25 deletions

View File

@@ -14,6 +14,9 @@ import {
} from "sequelize";
import dayjs from "dayjs";
import { t } from "i18next";
import log from "electron-log/main";
const logger = log.scope("db/handlers/recordings-handler");
class RecordingsHandler {
private async findAll(
@@ -64,6 +67,36 @@ class RecordingsHandler {
});
}
private async syncAll(event: IpcMainEvent) {
const recordings = await Recording.findAll({
where: { syncedAt: null },
});
if (recordings.length == 0) return;
event.sender.send("on-notification", {
type: "warning",
message: t("syncingRecordings", { count: recordings.length }),
});
try {
recordings.forEach(async (recording) => {
await recording.sync();
});
event.sender.send("on-notification", {
type: "info",
message: t("allRecordingsSynced"),
});
} catch (err) {
logger.error("failed to sync recordings", err.message);
event.sender.send("on-notification", {
type: "error",
message: t("failedToSyncRecordings"),
});
}
}
private async create(
event: IpcMainEvent,
options: Attributes<Recording> & {
@@ -293,7 +326,7 @@ class RecordingsHandler {
private async groupBySegment(
event: IpcMainEvent,
targetId: string,
targetType: string,
targetType: string
) {
return Recording.findAll({
where: {
@@ -328,6 +361,7 @@ class RecordingsHandler {
register() {
ipcMain.handle("recordings-find-all", this.findAll);
ipcMain.handle("recordings-find-one", this.findOne);
ipcMain.handle("recordings-sync-all", this.syncAll);
ipcMain.handle("recordings-create", this.create);
ipcMain.handle("recordings-destroy", this.destroy);
ipcMain.handle("recordings-upload", this.upload);

View File

@@ -16,7 +16,7 @@ import {
} from "sequelize-typescript";
import { Recording, Speech, Transcription, Video } from "@main/db/models";
import settings from "@main/settings";
import { AudioFormats, VideoFormats , WEB_API_URL } from "@/constants";
import { AudioFormats, VideoFormats, WEB_API_URL } from "@/constants";
import { hashFile } from "@/utils";
import path from "path";
import fs from "fs-extra";
@@ -170,7 +170,7 @@ export class Audio extends Model<Audio> {
const webApi = new Client({
baseUrl: process.env.WEB_API_URL || WEB_API_URL,
accessToken: settings.getSync("user.accessToken") as string,
logger: log.scope("api/client"),
logger: log.scope("audio/sync"),
});
return webApi.syncAudio(this.toJSON()).then(() => {
@@ -231,6 +231,16 @@ export class Audio extends Model<Audio> {
targetType: "Audio",
},
});
const webApi = new Client({
baseUrl: process.env.WEB_API_URL || WEB_API_URL,
accessToken: settings.getSync("user.accessToken") as string,
logger: log.scope("audio/cleanupFile"),
});
webApi.deleteAudio(audio.id).catch((err) => {
logger.error("deleteAudio failed:", err.message);
});
}
static async buildFromLocalFile(

View File

@@ -139,7 +139,7 @@ export class Recording extends Model<Recording> {
const webApi = new Client({
baseUrl: process.env.WEB_API_URL || WEB_API_URL,
accessToken: settings.getSync("user.accessToken") as string,
logger: log.scope("api/client"),
logger: log.scope("recording/sync"),
});
return webApi.syncRecording(this.toJSON()).then(() => {
@@ -160,7 +160,7 @@ export class Recording extends Model<Recording> {
const webApi = new Client({
baseUrl: process.env.WEB_API_URL || WEB_API_URL,
accessToken: settings.getSync("user.accessToken") as string,
logger: log.scope("api/client"),
logger: log.scope("recording/assess"),
});
const { token, region } = await webApi.generateSpeechToken();
@@ -274,6 +274,14 @@ export class Recording extends Model<Recording> {
@AfterDestroy
static cleanupFile(recording: Recording) {
fs.remove(recording.filePath);
const webApi = new Client({
baseUrl: process.env.WEB_API_URL || WEB_API_URL,
accessToken: settings.getSync("user.accessToken") as string,
logger: log.scope("recording/cleanupFile"),
});
webApi.deleteRecording(recording.id).catch((err) => {
logger.error("deleteRecording failed:", err.message);
});
}
static async createFromBlob(

View File

@@ -16,7 +16,7 @@ import {
} from "sequelize-typescript";
import { Audio, Recording, Speech, Transcription } from "@main/db/models";
import settings from "@main/settings";
import { AudioFormats, VideoFormats , WEB_API_URL } from "@/constants";
import { AudioFormats, VideoFormats, WEB_API_URL } from "@/constants";
import { hashFile } from "@/utils";
import path from "path";
import fs from "fs-extra";
@@ -192,7 +192,7 @@ export class Video extends Model<Video> {
const webApi = new Client({
baseUrl: process.env.WEB_API_URL || WEB_API_URL,
accessToken: settings.getSync("user.accessToken") as string,
logger: log.scope("api/client"),
logger: log.scope("video/sync"),
});
return webApi.syncVideo(this.toJSON()).then(() => {
@@ -254,6 +254,16 @@ export class Video extends Model<Video> {
targetType: "Video",
},
});
const webApi = new Client({
baseUrl: process.env.WEB_API_URL || WEB_API_URL,
accessToken: settings.getSync("user.accessToken") as string,
logger: log.scope("video/cleanupFile"),
});
webApi.deleteVideo(video.id).catch((err) => {
logger.error("deleteAudio failed:", err.message);
});
}
static async buildFromLocalFile(