Skip to content

Commit

Permalink
file rename fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Specy committed May 10, 2023
1 parent a602b38 commit c8884a2
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 43 deletions.
36 changes: 19 additions & 17 deletions client/src/components/ElementRow.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import FaStop from 'svelte-icons/fa/FaStop.svelte';
import FaEdit from 'svelte-icons/fa/FaEdit.svelte';
import prettyBytes from 'pretty-bytes';
import { capitalize, clamp, toResourceUrl } from '$lib/utils';
import { capitalize, clamp, getFileFormat, removeFileFormat, toResourceUrl } from '$lib/utils';
import Icon from './layout/Icon.svelte';
import FaCog from 'svelte-icons/fa/FaCog.svelte';
import { createEventDispatcher } from 'svelte';
Expand All @@ -23,27 +23,24 @@
import ElementSettingsRow from './Settings/ElementSettingsRow.svelte';
export let element: ConversionFile<FileTypes>;
let type = element.getType();
let path = toResourceUrl(element.file.path);
let scaleFactor = 2;
let videoRef: HTMLVideoElement;
let settingsOpen = false;
let fileFormat = (/[^./\\]*$/.exec(element.finalName) || [''])[0];
$: fileFormat = getFileFormat(element.finalName);
$: fileName = removeFileFormat(element.finalName);
$: type = element.getType();
$: path = toResourceUrl(element.file.path);
$: status = element.status.status;
$: scaleFactor = element.settings.opts.values.scale ?? $schemaStore.currentGlobalSettings!.scale ?? 2;
const dispatcher = createEventDispatcher<{
delete: undefined;
showResult: ConversionDiff;
nameChange: string;
formatChange: string;
}>();
$: {
type = element.getType();
path = toResourceUrl(element.file.path);
}
let status = element.status.status;
let upscaler = element.settings.upscaler ?? $schemaStore.currentUpscaler;
let schema = createDerivedSchemaStore(upscaler, type);
$: fileFormat = (/[^./\\]*$/.exec(element.finalName) || [''])[0];
$: status = element.status.status;
$: scaleFactor = element.settings.opts.values.scale ?? $schemaStore.currentGlobalSettings!.scale;
$: {
if (element.settings.upscaler !== upscaler) {
let newUpscaler = element.settings.upscaler ?? $schemaStore.currentUpscaler;
Expand All @@ -52,7 +49,11 @@
}
}
function onNameChange(e: Event) {
element.finalName = (e.target as HTMLDivElement).innerText;
dispatcher('nameChange', (e.target as HTMLDivElement).innerText);
}
$: {
fileFormat = getFileFormat(element.finalName);
fileName = removeFileFormat(element.finalName);
}
</script>

Expand Down Expand Up @@ -90,15 +91,15 @@
on:input={onNameChange}
class="content-editable-name"
>
{element.finalName.replace(/.[^./\\]*$/, '')}
{fileName}
</div>
{#if ['image', 'webp'].includes(type)}
<FormatPicker
formats={['png', 'webp', 'jpg', 'jpeg']}
force
value={fileFormat}
on:change={(e) => {
element.finalName = element.finalName.replace(/[^./\\]*$/, e.detail);
dispatcher('formatChange', e.detail);
}}
/>
{:else}
Expand Down Expand Up @@ -178,6 +179,7 @@
if (status === Status.Converting) {
window.api.haltOne(element.serialize());
} else {
console.log('executing', element.serialize());
window.api.executeFiles(
[element.serialize()],
schemaStore.getSerializedGlobalSettings(),
Expand Down Expand Up @@ -213,7 +215,7 @@
<ElementSettingsRow
isDefault={element.settings.upscaler === undefined}
on:reset={() => {
element.setUpscaler(undefined)
element.setUpscaler(undefined);
element = element;
}}
title="Upscaler"
Expand Down
6 changes: 6 additions & 0 deletions client/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ export function delay(ms:number){
return new Promise(resolve => setTimeout(resolve, ms))
}

export function getFileFormat(fileName: string){
return (/(\.\w*)$/.exec(fileName) || [''])[0].replace('.', '')
}
export function removeFileFormat(fileName: string){
return fileName.replace(/(\.\w*)$/, '')
}
13 changes: 11 additions & 2 deletions client/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import type { FileTypes } from '$common/types/Files';
import SettingsRenderer from '$cmp/Settings/SettingsRenderer.svelte';
import Select from '$cmp/inputs/Select.svelte';
import { capitalize } from '$lib/utils';
import { capitalize, getFileFormat, removeFileFormat } from '$lib/utils';
import ElementRow from '$cmp/ElementRow.svelte';
let floatingResult: ConversionDiff | undefined;
let isProcessing = false;
Expand Down Expand Up @@ -63,6 +63,7 @@
window.removeEventListener('keydown', onKeyDown);
};
});
</script>

<div class="page">
Expand Down Expand Up @@ -161,14 +162,22 @@
No files selected, go add some!
</div>
{:else if $schemaStore.schema}
{#each $conversionsStore.files.slice(0, perPage * page) as el (el.id)}
{#each $conversionsStore.files.slice(0, perPage * page) as el, i (el.id)}
<div animate:flip={{ duration: 200 }} in:slide|local out:fade|local>
<ElementRow
element={el}
on:delete={() => conversionsStore.remove(el)}
on:showResult={(data) => {
floatingResult = data.detail;
}}
on:formatChange={(e) => {
const nameWithoutFormat = removeFileFormat(el.finalName);
el.finalName = `${nameWithoutFormat}.${e.detail}`;
}}
on:nameChange={(e) => {
const format = getFileFormat(el.finalName);
el.finalName = `${e.detail}.${format}`;
}}
/>
</div>
{/each}
Expand Down
1 change: 1 addition & 0 deletions client/src/stores/conversionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ function createConversionsStore() {
remove,
updateStatus,
getNextValid,
set
}
}

Expand Down
14 changes: 9 additions & 5 deletions client/src/stores/settingsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ function createValue<T>(name: string, value: T) {
const baseValues: SettingValues = {
maxConcurrentOperations: createValue("Max concurrent operations", 4),
maxConcurrentFrames: createValue("Max concurrent frames", 4),
outputDirectory: createValue("Output directory", "./results/"),
outputDirectory: createValue("Output directory", ""),
saveInDatedFolder: createValue("Save in folders with dates", true),
appendUpscaleSettingsToFileName: createValue("Append upscale settings to the file name", true)
}

const debouncer = createDebouncer(1000)
const CURRENT_VERSION = "1.0.4"
const CURRENT_VERSION = "1.0.7"
function createSettingsStore() {
const { subscribe, update, set } = writable<SettingValues>(baseValues)
let current = baseValues
Expand All @@ -58,11 +58,15 @@ function createSettingsStore() {
localStorage.setItem("scapix_settings", JSON.stringify(toStore))
})
}
function fetch() {
async function fetch() {
try {
const stored = JSON.parse(localStorage.getItem("scapix_settings") ?? "null") as Settings
if (!stored) return store()
if (stored?.meta?.version !== CURRENT_VERSION) return store()
if(!stored || stored?.meta?.version !== CURRENT_VERSION){
const settings = {...current}
settings.outputDirectory.value = await window.api.getPathOf("default-results")
store()
return
}
set(stored.values)
} catch (e) {
console.error(e)
Expand Down
11 changes: 7 additions & 4 deletions electron/src/client/ipc/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { contextBridge, ipcRenderer as ipc } from "electron";
import { SerializedConversionFile, SerializedSettings, StatusUpdate } from "../../common/types/Files";
import { SerializedConversionFile, SerializedSettings, SpecialPathName, StatusUpdate } from "../../common/types/Files";
import { AppSchema, GlobalSettings, OptionalUpscaleSettings, SchemaType } from "upscalers/upscalers.interface";
type EventListener = {
id: string,
Expand Down Expand Up @@ -58,9 +58,9 @@ const controls = {
addOnLog: (callback: (type: "error" | "log" | "warn", message: string, timeout?: number) => void) => {
const id = EventListeners.generateId()
const listener = {
id,
id,
callback: (e: any, data: any) => {
const {type, message, timeout} = data
const { type, message, timeout } = data
callback(type, message, timeout)
}
}
Expand All @@ -76,7 +76,7 @@ const controls = {
const listener = eventListeners.removeListener("maximize-change", id);
if (!listener) return;
ipc.removeListener("maximize-change", listener.callback);
}
},
}
export type Controls = typeof controls;
contextBridge.exposeInMainWorld("controls", controls)
Expand Down Expand Up @@ -126,6 +126,9 @@ const api = {
const listener = eventListeners.removeListener("file-status-change", id);
if (!listener) return;
ipc.removeListener("file-status-change", listener.callback);
},
getPathOf: async (specialPath: SpecialPathName): Promise<string> => {
return ipc.invoke("get-special-path", specialPath)
}
}

Expand Down
3 changes: 3 additions & 0 deletions electron/src/common/types/Files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export type {
UpscalerName,
OptionalUpscaleSettings
}

export type SpecialPathName = "default-results" | "models-folder"

export enum Status {
Idle = "idle",
Waiting = "waiting",
Expand Down
14 changes: 12 additions & 2 deletions electron/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { app, BrowserWindow, ipcMain as ipc, protocol, dialog, shell } from "ele
import url from "url";
import path from "path";
import { AsyncSemaphore, PATHS, ROOT_PATH } from "./utils";
import { DenoiseLevel, SerializedConversionFile, SerializedSettings, Status } from "./common/types/Files";
import { DenoiseLevel, SerializedConversionFile, SerializedSettings, SpecialPathName, Status } from "./common/types/Files";
import Waifu2x from "waifu2x";
import fs from "fs/promises"
import serve from "electron-serve";
Expand All @@ -12,7 +12,6 @@ import log from "electron-log";
import { AppSchema, GlobalSettings, OptionalUpscaleSettings, SchemaType, UpscalerResult, upscalerHandler } from "./upscalers/upscalers.interface";
const isDev = !app.isPackaged


try {
log.transports.file.resolvePath = () => path.join(ROOT_PATH, 'logs/main.log');
Object.assign(console, log.functions)
Expand Down Expand Up @@ -159,6 +158,17 @@ function setUpIpc(win: BrowserWindow) {
if(!data.ok) throw new Error(data.error)
return data.value;
})
ipc.handle("get-special-path", async (e, specialPath: SpecialPathName) => {
switch (specialPath){
case "default-results": {
const result = path.join(app.getPath("documents"), "Scapix", "results")
await fs.mkdir(result, {recursive: true })
return result;
}
case "models-folder": return PATHS.models
}
return ""
})
ipc.handle("halt-one-execution", (e, idOrFile: string | SerializedConversionFile) => {
const id = typeof idOrFile === "string" ? idOrFile : idOrFile.id;
const file = pendingFiles.get(id);
Expand Down
1 change: 1 addition & 0 deletions electron/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const PATHS = {
electronDist: path.join(ROOT_PATH, "/electron/dist"),
electronClient: path.join(ROOT_PATH, "/electron/dist/client"),
electronStatic: path.join(ROOT_PATH, "/electron/static"),
models: path.join(ROOT_PATH, "/models"),
waifu2xModels: path.join(ROOT_PATH, "/models/waifu2x"),
ffmpeg: ffmpeg.path
}
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@electron-forge/maker-wix": "^6.1.1",
"@electron-forge/maker-zip": "^6.1.1",
"concurrently": "^8.0.1",
"electron": "^24.1.3",
"electron": "^24.2.0",
"electron-reloader": "^1.2.3",
"typescript": "^5.0.4"
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.tsbuildinfo

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1141,10 +1141,10 @@
optionalDependencies:
"@bitdisaster/exe-icon-extractor" "^1.0.10"

"electron@^24.1.3":
"integrity" "sha512-lHFRf2CgeOxPLje4BayzaLz1e/SbNejUU4NDarFjBlcYfuH4ceGevYRVjyDZJLbSGdhmM2dLraeku30LGEigCg=="
"resolved" "https://registry.npmjs.org/electron/-/electron-24.1.3.tgz"
"version" "24.1.3"
"electron@^24.2.0":
"integrity" "sha512-fEYAftYqFhveniWJbEHXjNMWjooFFIuqNj/eEFJkGzycInfBJq/c4E/dew++s6s0YLubxFnjoF2qZiqapLj0gA=="
"resolved" "https://registry.npmjs.org/electron/-/electron-24.2.0.tgz"
"version" "24.2.0"
dependencies:
"@electron/get" "^2.0.0"
"@types/node" "^18.11.18"
Expand Down

0 comments on commit c8884a2

Please sign in to comment.