Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ./routes/utils.ts functions to use object for O(1) lookups #51

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions svelte/src/routes/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,11 @@ function getFileUrl(comfyUrl: string, folderType: string, file: any) {
}
}

function findFile(filename: string, exts: Array<string>, files: Array<any>) {
let fn: any = filename.split('.');
fn.pop();
fn = fn.join('.');
return files.find((f: any) => {
const fa = f.name.split('.');
const extname = fa.pop().toLowerCase();
const name = fa.join('.');

return name === fn && exts.includes(extname);
});
function findFile(filename: string, exts: Array<string>, filesMap: Array<any>) {
if (filesMap.allFiles.hasOwnProperty(filename)) {
return filesMap.allFiles[filename];
}
return false;
}

function processFile(
Expand All @@ -41,11 +35,12 @@ function processFile(
if (WHITE_EXTS.includes(extname)) {
file['fileType'] = extname;
if (extname === 'json') {
if (findFile(file.name, IMAGE_EXTS.concat(VIDEO_EXTS), files)) {
if (findFile(file.fullPath, IMAGE_EXTS.concat(VIDEO_EXTS), files)) {
return;
}
}
}

if (IMAGE_EXTS.includes(extname)) {
file['fileType'] = 'image';
}
Expand All @@ -56,11 +51,15 @@ function processFile(
return;
}

file['url'] = getFileUrl(comfyUrl, folderType, file);
if (['image', 'video'].includes(file['fileType'])) {
file['previewUrl'] = getFileUrl(comfyUrl, folderType, file);

let jsonFile = findFile(file.name, JSON_EXTS, files);
var url = getFileUrl(comfyUrl, folderType, file);
file['url'] = url;
if (['image', 'video'].includes(file['fileType'])) {
file['previewUrl'] = url;
var path = file.fullPath;
var jsonFileName = path.substring(0, path.lastIndexOf('.')) || path;
var jsonFileName = jsonFileName + ".json";
let jsonFile = findFile(jsonFileName, JSON_EXTS, files);
if (jsonFile) {
file['url'] = getFileUrl(comfyUrl, folderType, jsonFile);
}
Expand Down Expand Up @@ -98,15 +97,30 @@ export async function fetchFiles(

const res = await fetch(url);
const ret = await res.json();

let files = ret.files;
let newFiles: Array<any> = [];
var map = {
"directories": {},
"allFiles": {}
};

var filesMap = files.reduce(function(map, obj) {
var dir = (obj.type == 'dir') ? "" : obj.folder_path;
var filePath = "/" + dir + obj.name;
obj.fullPath = filePath;
map.allFiles[filePath] = obj;
if (obj.type == 'dir') {
map.directories[filePath] = obj;
}
return map;
}, map);

files.forEach((f: any) => {
let newFile;
if (f.type === 'dir') {
newFile = processDir(f);
} else {
newFile = processFile(f, folderType, comfyUrl, files);
newFile = processFile(f, folderType, comfyUrl, filesMap);
}

if (newFile) {
Expand Down

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

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

1 change: 1 addition & 0 deletions web/build/_app/immutable/chunks/preload-helper.0HuHagjb.js

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

1 change: 0 additions & 1 deletion web/build/_app/immutable/chunks/preload-helper.Dch09mLN.js

This file was deleted.

Large diffs are not rendered by default.

Loading