{"record":{"id":"17793ddb788438d5","repo":"LykosAI/StabilityMatrix","slug":"model-file-name-must-contain-a-valid-file-name","errorCode":null,"errorMessage":"Model file name must contain a valid file name.","messagePattern":"Model file name must contain a valid file name\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Services/ModelImportService.cs","lineNumber":291,"sourceCode":"        Uri? previewImageUri = null,\n        string? previewImageFileExtension = null,\n        ConnectedModelInfo? connectedModelInfo = null,\n        Action<TrackedDownload>? configureDownload = null\n    )\n    {\n        // Subfolder support for user-defined patterns such as\n        // \"{base_model}/{model_name}/{file_name}\". Treat every component as relative so\n        // rooted or traversal input cannot escape the selected models folder.\n        var pathSegments = modelFileName\n            .Split(['/', '\\\\'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)\n            .Where(segment => segment is not \".\" and not \"..\")\n            .Select(SanitizePathSegment)\n            .Where(segment => !string.IsNullOrWhiteSpace(segment))\n            .ToArray();\n\n        if (pathSegments.Length == 0)\n        {\n            throw new ArgumentException(\n                \"Model file name must contain a valid file name.\",\n                nameof(modelFileName)\n            );\n        }\n\n        modelFileName = pathSegments[^1];\n        if (pathSegments.Length > 1)\n        {\n            downloadFolder = new DirectoryPath(\n                [downloadFolder.FullPath, .. pathSegments.Take(pathSegments.Length - 1)]\n            );\n        }\n\n        // Folders might be missing if user didn't install any packages yet\n        downloadFolder.Create();\n\n        // Fix invalid chars in FileName\n        var modelBaseFileName = SanitizePathSegment(Path.GetFileNameWithoutExtension(modelFileName));","sourceCodeStart":273,"sourceCodeEnd":309,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Services/ModelImportService.cs#L273-L309","documentation":"DoCustomImport in ModelImportService throws this ArgumentException when, after splitting the model file name into sanitized path segments, no non-empty segment remains — meaning the provided modelFileName contains no usable file name (only separators, traversal segments, or whitespace).","triggerScenarios":"Calling DoCustomImport with modelFileName like \"\", \"///\", \"..\", \".\", or a string of only invalid path characters; also when a custom pattern strips everything out.","commonSituations":"Programmatic imports passing an empty name from upstream metadata; URLs whose last path segment is empty or a query fragment; user-supplied names that sanitize to nothing; copy-paste of directory-only paths.","solutions":["Pass a concrete model file name with an extension (e.g. \"model.safetensors\") to DoCustomImport","Strip query strings/URL fragments and take the last real path segment before calling","Validate that the file name contains non-whitespace, non-separator characters before importing","For Civitai/URL downloads, derive the file name from the download URL or metadata first"],"exampleFix":"// before\nawait importService.DoCustomImport(uri, \"///\", localModelFile, modelDbModel);\n// after\nvar fileName = new Uri(uri).Segments[^1].Trim('/', '?');\nif (string.IsNullOrWhiteSpace(fileName))\n    throw new ArgumentException(\"Provide a valid model file name\");\nawait importService.DoCustomImport(uri, fileName, localModelFile, modelDbModel);","handlingStrategy":"validation","validationCode":"var segments = modelFileName.Split('/', '\\\\', Path.DirectorySeparatorChar)\n    .Select(s => s.Trim()).Where(s => !string.IsNullOrWhiteSpace(s) && s != \".\" && s != \"..\").ToArray();\nif (segments.Length == 0)\n    throw new ArgumentException(\"Provide a valid model file name\");","typeGuard":"bool IsValidModelFileName(string name) =>\n    !string.IsNullOrWhiteSpace(name) &&\n    name.Split('/', '\\\\').Select(s => s.Trim())\n        .Any(s => !string.IsNullOrWhiteSpace(s) && s != \".\" && s != \"..\");","tryCatchPattern":"try\n{\n    await importService.DoCustomImport(downloadUri, modelFileName, localModelFile, modelDbModel);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"valid file name\"))\n{\n    // derive name from URL or ask user for a file name\n}","preventionTips":["Always pass concrete file names with extensions","Sanitize and validate names before calling import APIs","Derive names from URL last segment minus query strings"],"tags":["import","filename","validation","argument"],"backgroundTag":"empty-required-field","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}