Skip to content

Commit

Permalink
Add non-temp fallback directory for video cache (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbonner committed Oct 18, 2024
1 parent bd3dc7c commit 2c3e543
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/main/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const AUTO_ASSETS = path.join(app.getPath("userData"), "autoAssets");
export const DEFAULT_USER_ASSETS = path.join(app.getPath("userData"), "userAssets");
export const LEGACY_ASSETS = path.join(app.getPath("userData"), "frcData");
export const LAST_OPEN_FILE = path.join(app.getPath("temp"), "akit-log-path.txt");
export const VIDEO_CACHE = path.join(app.getPath("temp"), "advantagescope-videos");
export const VIDEO_CACHE = path.join(app.getPath("temp"), "advantagescope-video-cache");
export const VIDEO_CACHE_FALLBACK = path.join(app.getPath("userData"), "video-cache");
export const FRC_LOG_FOLDER = "C:\\Users\\Public\\Documents\\FRC\\Log Files";
export const WINDOW_ICON = process.platform === "darwin" ? undefined : path.join(__dirname, "../icons/window-icon.png");
export const DEFAULT_PREFS: Preferences = {
Expand Down
39 changes: 23 additions & 16 deletions src/main/VideoProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Preferences from "../shared/Preferences";
import { getTBAMatchInfo, getTBAMatchKey } from "../shared/TBAUtil";
import VideoSource from "../shared/VideoSource";
import { createUUID, zfill } from "../shared/util";
import { PREFS_FILENAME, VIDEO_CACHE, WINDOW_ICON } from "./Constants";
import { PREFS_FILENAME, VIDEO_CACHE, VIDEO_CACHE_FALLBACK, WINDOW_ICON } from "./Constants";

export class VideoProcessor {
private static NUM_TESSERACT_WORKERS = 8;
Expand Down Expand Up @@ -94,12 +94,12 @@ export class VideoProcessor {
menuCoordinates: null | [number, number],
callback: (data: any) => void
) {
let loadPath = (videoPath: string) => {
let loadPath = (videoPath: string, videoCache: string) => {
// Indicate download has started
callback({ uuid: uuid });

// Create cache folder
let cachePath = path.join(VIDEO_CACHE, createUUID()) + path.sep;
let cachePath = path.join(videoCache, createUUID()) + path.sep;
if (fs.existsSync(cachePath)) {
fs.rmSync(cachePath, { recursive: true });
}
Expand Down Expand Up @@ -296,7 +296,12 @@ export class VideoProcessor {
matchStartFrame: matchStartFrame
});
} else if (code === 1) {
sendError();
if (videoCache === VIDEO_CACHE && fullOutput.includes("No space left on device")) {
fs.rmSync(cachePath, { recursive: true });
loadPath(videoPath, VIDEO_CACHE_FALLBACK);
} else {
sendError();
}
}
});
};
Expand All @@ -305,7 +310,7 @@ export class VideoProcessor {
switch (source) {
case VideoSource.Local:
this.getLocalPath(window)
.then(loadPath)
.then((path) => loadPath(path, VIDEO_CACHE))
.catch(() => {});
break;
case VideoSource.YouTube:
Expand All @@ -321,7 +326,7 @@ export class VideoProcessor {
});
} else {
this.getDirectUrlFromYouTubeUrl(clipboardText)
.then(loadPath)
.then((path) => loadPath(path, VIDEO_CACHE))
.catch(() => {
callback({ uuid: uuid, error: true });
dialog.showMessageBox(window, {
Expand All @@ -339,7 +344,7 @@ export class VideoProcessor {
this.getYouTubeUrlFromMatchInfo(matchInfo!, window, menuCoordinates!)
.then((url) => {
this.getDirectUrlFromYouTubeUrl(url)
.then(loadPath)
.then((path) => loadPath(path, VIDEO_CACHE))
.catch(() => {
callback({ uuid: uuid, error: true });
dialog.showMessageBox(window, {
Expand Down Expand Up @@ -487,16 +492,18 @@ export class VideoProcessor {
Object.values(VideoProcessor.processes).forEach((process) => {
process.kill();
});
if (fs.existsSync(VIDEO_CACHE)) {
try {
fs.rmSync(VIDEO_CACHE, { recursive: true });
} catch {
// ffmpeg might not have shut down completely, and "rmSync" will
// sometimes throw an exception if files are still being written.
// Fail silently instead of crashing since the OS will clear the
// few remaining images automatically (or we will on the next shutdown).
[VIDEO_CACHE, VIDEO_CACHE_FALLBACK].forEach((videoCache) => {
if (fs.existsSync(videoCache)) {
try {
fs.rmSync(videoCache, { recursive: true });
} catch {
// ffmpeg might not have shut down completely, and "rmSync" will
// sometimes throw an exception if files are still being written.
// Fail silently instead of crashing since the OS will clear the
// few remaining images automatically (or we will on the next shutdown).
}
}
}
});
}

// https://github.com/sindresorhus/video-extensions
Expand Down

0 comments on commit 2c3e543

Please sign in to comment.