Skip to content

Commit 4fe13ef

Browse files
committed
feat(storage): allow #a=1&b=2 params; HTTP can use #fname=XXX
HTTPStorage: Consider https://civitai.com/api/download/models/15603 Which has a content-disposition of "Light and Shadow.safetensors" But we don't know that in advance, so we'd have to perform the request each time to see if the file is already cached locally. To avoid that, we can now give a URL like http://.../models/15603#fname=light-and-shadow.safetensors Which will check the cache against this filename. Notes: * Hash query is never sent to to the server. * Application code might still manipulate the filename, e.g. in the above case, the final name might be `lora_weights--light_and_shadow.safetensors` in the diffusers cache directory.
1 parent a40129a commit 4fe13ef

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

api/utils/storage/BaseStorage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class BaseArchive(ABC):
99
def __init__(self, path, status=None):
1010
self.path = path
1111
self.status = status
12+
self.query = {}
1213

1314
def updateStatus(self, type, progress):
1415
if self.status:

api/utils/storage/HTTPStorage.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import requests
55
from tqdm import tqdm
66
from .BaseStorage import BaseStorage
7+
import urllib.parse
78

89

910
def get_now():
@@ -17,6 +18,10 @@ def test(url):
1718

1819
def __init__(self, url, **kwargs):
1920
super().__init__(url, **kwargs)
21+
parts = self.url.split("#", 1)
22+
self.url = parts[0]
23+
if len(parts) > 1:
24+
self.query = urllib.parse.parse_qs(parts[1])
2025

2126
def upload_file(self, source, dest):
2227
raise RuntimeError("HTTP PUT not implemented yet")
@@ -25,13 +30,13 @@ def download_file(self, fname):
2530
print(f"Downloading {self.url} to {fname}...")
2631
resp = requests.get(self.url, stream=True)
2732
total = int(resp.headers.get("content-length", 0))
28-
content_disposition = resp.headers.get("content-disposition")
33+
content_disposition = resp.headers.get("content-disposition")
2934
if content_disposition:
3035
filename_search = re.search('filename="(.+)"', content_disposition)
3136
if filename_search:
3237
self.filename = filename_search.group(1)
3338
else:
34-
print('Warning: content-disposition header is not found in the response.')
39+
print("Warning: content-disposition header is not found in the response.")
3540
# Can also replace 'file' with a io.BytesIO object
3641
with open(fname, "wb") as file, tqdm(
3742
desc="Downloading",

0 commit comments

Comments
 (0)