@@ -511,16 +511,17 @@ public static async Task<JObject> DoModelDownloadWS(Session session, WebSocket w
511
511
JObject metadataObj = JObject . Parse ( metadata ) ;
512
512
baseModel = metadataObj [ "modelspec.baseModel" ] ? . ToString ( ) ?? "" ;
513
513
}
514
- string outPath ;
515
- if ( ! string . IsNullOrWhiteSpace ( baseModel ) )
514
+ string modelOutPath ;
515
+ if ( ! string . IsNullOrWhiteSpace ( baseModel ) && session . User . Settings . GroupDownloadedModelsByBaseType )
516
516
{
517
- outPath = $ "{ handler . FolderPaths [ 0 ] } /{ baseModel } /{ name } .safetensors";
517
+ modelOutPath = $ "{ handler . FolderPaths [ 0 ] } /{ baseModel } /{ name } .safetensors";
518
518
}
519
519
else
520
520
{
521
- outPath = $ "{ handler . FolderPaths [ 0 ] } /{ name } .safetensors";
521
+ modelOutPath = $ "{ handler . FolderPaths [ 0 ] } /{ name } .safetensors";
522
522
}
523
- if ( File . Exists ( outPath ) )
523
+ modelOutPath = Utilities . StrictFilenameClean ( outPath ) ;
524
+ if ( File . Exists ( modelOutPath ) )
524
525
{
525
526
await ws . SendJson ( new JObject ( ) { [ "error" ] = "Model at that save path already exists." } , API . WebsocketTimeout ) ;
526
527
return null ;
@@ -530,7 +531,7 @@ public static async Task<JObject> DoModelDownloadWS(Session session, WebSocket w
530
531
{
531
532
File . Delete ( tempPath ) ;
532
533
}
533
- Directory . CreateDirectory ( Path . GetDirectoryName ( outPath ) ) ;
534
+ Directory . CreateDirectory ( Path . GetDirectoryName ( modelOutPath ) ) ;
534
535
using CancellationTokenSource canceller = new ( ) ;
535
536
Task downloading = Utilities . DownloadFile ( url , tempPath , ( progress , total , perSec ) =>
536
537
{
@@ -570,17 +571,20 @@ public static async Task<JObject> DoModelDownloadWS(Session session, WebSocket w
570
571
}
571
572
} ) ;
572
573
await downloading ;
573
- File . Move ( tempPath , outPath ) ;
574
+ File . Move ( tempPath , modelOutPath ) ;
574
575
if ( ! string . IsNullOrWhiteSpace ( metadata ) )
575
576
{
576
- if ( ! string . IsNullOrWhiteSpace ( baseModel ) )
577
+ string metadataOutPath ;
578
+ if ( ! string . IsNullOrWhiteSpace ( baseModel ) && session . User . Settings . GroupDownloadedModelsByBaseType )
577
579
{
578
- File . WriteAllText ( $ "{ handler . FolderPaths [ 0 ] } /{ baseModel } /{ name } .json", metadata ) ;
580
+ metadataOutPath = $ "{ handler . FolderPaths [ 0 ] } /{ baseModel } /{ name } .json";
579
581
}
580
582
else
581
583
{
582
- File . WriteAllText ( $ "{ handler . FolderPaths [ 0 ] } /{ name } .json", metadata ) ;
584
+ metadataOutPath = $ "{ handler . FolderPaths [ 0 ] } /{ name } .json";
583
585
}
586
+ metadataOutPath = Utilities . StrictFilenameClean ( metadataOutPath ) ;
587
+ File . WriteAllText ( metadataOutPath , metadata ) ;
584
588
}
585
589
await ws . SendJson ( new JObject ( ) { [ "success" ] = true } , API . WebsocketTimeout ) ;
586
590
}
0 commit comments