Skip to content
Merged
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
12 changes: 10 additions & 2 deletions src/WebAPI/ModelsAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,12 @@ public static async Task<JObject> DoModelDownloadWS(Session session, WebSocket w
}
string originalUrl = url;
url = url.Before('#');
Dictionary<string, string> headers = [];
if (url.StartsWith("https://civitai.com/"))
{
url = $"https://civitai.red/{url["https://civitai.com/".Length..]}";
}
Dictionary<string, string> headers = [];
if (url.StartsWith("https://civitai.red/"))
{
string civitaiApiKey = session.User.GetGenericData("civitai_api", "key");
if (!string.IsNullOrEmpty(civitaiApiKey))
Expand Down Expand Up @@ -757,7 +761,11 @@ public static async Task<JObject> GetModelHash(Session session,
[API.APIDescription("Forwards a metadata request, eg to civitai API.", "")]
public static async Task<JObject> ForwardMetadataRequest(Session session, string url)
{
if (!url.StartsWithFast("https://civitai.com/"))
if (url.StartsWithFast("https://civitai.com/"))
{
url = $"https://civitai.red/{url["https://civitai.com/".Length..]}";
}
if (!url.StartsWithFast("https://civitai.red/"))
{
return new JObject() { ["error"] = "Invalid URL." };
}
Expand Down
8 changes: 7 additions & 1 deletion src/wwwroot/js/genpage/gentab/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,17 @@ function getCivitUrlGuessFor(model) {
}
let civitUrl = '';
// (Hacky but we don't have a dedicated datastore for this, just included at the top of descriptions generally)
let civitUrlStartIndex = model.description.indexOf('<a href="https://civitai.com/models/');
let civitUrlStartIndex = model.description.indexOf('<a href="https://civitai.red/models/');
if (civitUrlStartIndex == -1) {
civitUrlStartIndex = model.description.indexOf('<a href="https://civitai.com/models/');
}
if (civitUrlStartIndex != -1) {
let end = model.description.indexOf('"', civitUrlStartIndex + '<a href="'.length);
if (end != -1) {
civitUrl = model.description.substring(civitUrlStartIndex + '<a href="'.length, end);
if (civitUrl.startsWith('https://civitai.com/')) {
civitUrl = `https://civitai.red/${civitUrl.substring('https://civitai.com/'.length)}`;
}
if (!civitUrl.includes("?modelVersionId=") || civitUrl.length > 200 || civitUrl.includes("?modelVersionId=null")) {
console.log(`Invalid CivitAI URL (failed sanity check): ${civitUrl}`);
civitUrl = '';
Expand Down
29 changes: 19 additions & 10 deletions src/wwwroot/js/genpage/utiltab.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,23 @@ class ModelDownloaderUtil {
this.activeZone = getRequiredElementById('model_downloader_right_sidebar');
this.folders = getRequiredElementById('model_downloader_folder');
this.hfPrefix = 'https://huggingface.co/';
this.civitPrefix = 'https://civitai.com/';
this.civitPrefix = 'https://civitai.red/';
this.civitOldPrefix = 'https://civitai.com/';
this.civitGreenPrefix = 'https://civitai.green/';
this.urlRequestId = 0;
}

normalizeCivitaiUrl(url) {
url = url.trim();
if (url.startsWith(this.civitGreenPrefix)) {
return this.civitPrefix + url.substring(this.civitGreenPrefix.length);
}
if (url.startsWith(this.civitOldPrefix)) {
return this.civitPrefix + url.substring(this.civitOldPrefix.length);
}
return url;
}

buildFolderSelector(selector) {
if (!coreModelMap) {
return;
Expand Down Expand Up @@ -238,7 +250,7 @@ class ModelDownloaderUtil {
callback(null);
return;
}
callback(`https://civitai.com/models/${rawData.response.modelId}?modelVersionId=${rawData.response.id}`);
callback(`https://civitai.red/models/${rawData.response.modelId}?modelVersionId=${rawData.response.id}`);
}, 0, () => {
callback(null);
});
Expand Down Expand Up @@ -382,10 +394,7 @@ class ModelDownloaderUtil {
}

parseCivitaiUrl(url) {
url = url.trim();
if (url.startsWith(this.civitGreenPrefix)) {
url = this.civitPrefix + url.substring(this.civitGreenPrefix.length);
}
url = this.normalizeCivitaiUrl(url);
let parts = splitWithTail(url.substring(this.civitPrefix.length), '/', 4); // 'models', id, name + sometimes version OR 'api', 'download', 'models', versid
if (parts.length == 2 && parts[0] == 'models' && parts[1].includes('?')) {
let subparts = splitWithTail(parts[1], '?', 2);
Expand Down Expand Up @@ -475,7 +484,10 @@ class ModelDownloaderUtil {
this.metadataZone.dataset.raw = '';
delete this.metadataZone.dataset.image;
this.imageSide.innerHTML = '';
let url = this.url.value.trim();
let url = this.normalizeCivitaiUrl(this.url.value);
if (url != this.url.value) {
this.url.value = url;
}
this.urlRequestId++;
let requestId = this.urlRequestId;
if (url.endsWith('.pt') || url.endsWith('.pth') || url.endsWith('.ckpt') || url.endsWith('.bin')) {
Expand Down Expand Up @@ -519,9 +531,6 @@ class ModelDownloaderUtil {
this.button.disabled = false;
return;
}
if (url.startsWith(this.civitGreenPrefix)) {
url = this.civitPrefix + url.substring(this.civitGreenPrefix.length);
}
if (url.startsWith(this.civitPrefix)) {
let parts = splitWithTail(url.substring(this.civitPrefix.length), '/', 4); // 'models', id, name + sometimes version OR 'api', 'download', 'models', versid
if (parts.length == 2 && parts[0] == 'models' && parts[1].includes('?')) {
Expand Down
Loading