Skip to content

Commit

Permalink
gallery add http fallback
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Mandic <[email protected]>
  • Loading branch information
vladmandic committed Jan 16, 2025
1 parent 1bc4229 commit 6b225d0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

## Update for 2025-01-16

- **Gallery**:
- add http fallback for slow/unreliable links
- **Fixes**:
- non-full vae decode
- send-to image transfer
- sana vae tiling
- increase gallery timeouts

## Update for 2025-01-15

Expand Down
40 changes: 36 additions & 4 deletions javascript/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class GalleryFolder extends HTMLElement {
else folder.shadow.children[1].classList.remove('gallery-folder-selected');
}
});
div.addEventListener('click', fetchFiles); // eslint-disable-line no-use-before-define
div.addEventListener('click', fetchFilesWS); // eslint-disable-line no-use-before-define
this.shadow.appendChild(div);
}
}
Expand Down Expand Up @@ -224,7 +224,7 @@ async function getHash(str, algo = 'SHA-256') {
}
}

async function wsConnect(socket, timeout = 2000) {
async function wsConnect(socket, timeout = 5000) {
const intrasleep = 100;
const ttl = timeout / intrasleep;
const isOpened = () => (socket.readyState === WebSocket.OPEN);
Expand Down Expand Up @@ -327,19 +327,51 @@ async function gallerySort(btn) {
el.status.innerText = `Sort | ${arr.length.toLocaleString()} images | ${Math.floor(t1 - t0).toLocaleString()}ms`;
}

async function fetchFiles(evt) { // fetch file-by-file list over websockets
async function fetchFilesHT(evt) {
el.status.innerText = `Folder | ${evt.target.name}`;
const t0 = performance.now();
const fragment = document.createDocumentFragment();
el.status.innerText = `Folder | ${evt.target.name} | in-progress`;
let numFiles = 0;

const res = await fetch(`/sdapi/v1/browser/files?folder=${encodeURI(evt.target.name)}`);
if (!res || res.status !== 200) {
el.status.innerText = `Folder | ${evt.target.name} | failed: ${res?.statusText}`;
return;
}
const jsonData = await res.json();
for (const line of jsonData) {
const data = decodeURI(line).split('##F##');
numFiles++;
const f = new GalleryFile(data[0], data[1]);
fragment.appendChild(f);
}

el.files.appendChild(fragment);

const t1 = performance.now();
log(`gallery: folder=${evt.target.name} num=${numFiles} time=${Math.floor(t1 - t0)}ms`);
el.status.innerText = `Folder | ${evt.target.name} | ${numFiles.toLocaleString()} images | ${Math.floor(t1 - t0).toLocaleString()}ms`;
addSeparators();
}

async function fetchFilesWS(evt) { // fetch file-by-file list over websockets
el.files.innerHTML = '';
if (!url) return;
if (ws && ws.readyState === WebSocket.OPEN) ws.close(); // abort previous request
let wsConnected = false;
try {
ws = new WebSocket(`${url}/sdapi/v1/browser/files`);
wsConnected = await wsConnect(ws, 30000);
wsConnected = await wsConnect(ws);
} catch (err) {
log('gallery: ws connect error', err);
return;
}
log(`gallery: connected=${wsConnected} state=${ws?.readyState} url=${ws?.url}`);
if (!wsConnected) {
await fetchFilesHT(evt); // fallback to http
return;
}
el.status.innerText = `Folder | ${evt.target.name}`;
const t0 = performance.now();
let numFiles = 0;
Expand Down
20 changes: 19 additions & 1 deletion modules/api/gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,24 @@ async def get_thumb(file: str):
content = { 'error': str(e) }
return JSONResponse(content=content)

@app.get("/sdapi/v1/browser/files", response_model=list)
async def ht_files(folder: str):
try:
t0 = time.time()
files = files_cache.directory_files(folder, recursive=True)
lines = []
for f in files:
file = os.path.relpath(f, folder)
msg = quote(folder) + '##F##' + quote(file)
msg = msg[:1] + ":" + msg[4:] if msg[1:4] == "%3A" else msg
lines.append(msg)
t1 = time.time()
shared.log.debug(f'Gallery: type=ht folder="{folder}" files={len(lines)} time={t1-t0:.3f}')
return lines
except Exception as e:
shared.log.error(f'Gallery: {folder} {e}')
return []

@app.websocket("/sdapi/v1/browser/files")
async def ws_files(ws: WebSocket):
try:
Expand All @@ -173,7 +191,7 @@ async def ws_files(ws: WebSocket):
await manager.send(ws, msg)
await manager.send(ws, '#END#')
t1 = time.time()
shared.log.debug(f'Gallery: folder="{folder}" files={numFiles} time={t1-t0:.3f}')
shared.log.debug(f'Gallery: type=ws folder="{folder}" files={numFiles} time={t1-t0:.3f}')
except WebSocketDisconnect:
debug('Browser WS unexpected disconnect')
manager.disconnect(ws)

0 comments on commit 6b225d0

Please sign in to comment.