Browse Source

Throw DocumentNotFoundException when file not exists

pull/3641/head
liangshiwei 7 years ago
parent
commit
96324916b4
  1. 5
      modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentSource.cs
  2. 3
      modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml
  3. 65
      modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs
  4. 21
      modules/docs/src/Volo.Docs.Web/Pages/Documents/Shared/ErrorComponent/Default.cshtml
  5. 2
      modules/docs/src/Volo.Docs.Web/Pages/Documents/Shared/ErrorComponent/ErrorPageModel.cs
  6. 2
      modules/docs/src/Volo.Docs.Web/Pages/Documents/Shared/ErrorComponent/error.js

5
modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentSource.cs

@ -87,6 +87,11 @@ namespace Volo.Docs.FileSystem.Documents
{ {
throw new SecurityException("Can not get a resource file out of the project folder!"); throw new SecurityException("Can not get a resource file out of the project folder!");
} }
if (!File.Exists(path))
{
throw new DocumentNotFoundException(path);
}
} }
} }
} }

3
modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml

@ -371,7 +371,8 @@ else
RedirectUrl = Model.DocumentsUrlPrefix + Model.LanguageCode + "/" + Model.ProjectName + "/" RedirectUrl = Model.DocumentsUrlPrefix + Model.LanguageCode + "/" + Model.ProjectName + "/"
+ (Model.LatestVersionInfo.IsSelected ? DocsAppConsts.Latest : Model.Version), + (Model.LatestVersionInfo.IsSelected ? DocsAppConsts.Latest : Model.Version),
ErrorCode = "404", ErrorCode = "404",
ErrorMessage = L.GetString("DocumentNotFound") ErrorMessage = L.GetString("DocumentNotFound"),
AutoRedirect = !Model.DocumentName.IsNullOrWhiteSpace()
} }
})) }))
} }

65
modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs

@ -34,7 +34,7 @@ namespace Volo.Docs.Pages.Documents.Project
public string LanguageCode { get; set; } public string LanguageCode { get; set; }
public bool DocumentFound { get; set; } = true; public bool DocumentFound { get; set; } = true;
public bool ProjectFound { get; set; } = true; public bool ProjectFound { get; set; } = true;
public bool LoadSuccess => DocumentFound && ProjectFound; public bool LoadSuccess => DocumentFound && ProjectFound;
@ -347,43 +347,13 @@ namespace Volo.Docs.Pages.Documents.Project
try try
{ {
if (DocumentName.IsNullOrWhiteSpace()) Document = await GetSpecificDocumentOrDefaultAsync(LanguageCode);
{
Document = await _documentAppService.GetDefaultAsync(
new GetDefaultDocumentInput
{
ProjectId = Project.Id,
LanguageCode = LanguageCode,
Version = Version
}
);
}
else
{
Document = await _documentAppService.GetAsync(
new GetDocumentInput
{
ProjectId = Project.Id,
Name = DocumentNameWithExtension,
LanguageCode = LanguageCode,
Version = Version
}
);
}
} }
catch (DocumentNotFoundException) catch (DocumentNotFoundException)
{ {
if (LanguageCode != DefaultLanguageCode) if (LanguageCode != DefaultLanguageCode)
{ {
Document = await _documentAppService.GetAsync( Document = await GetSpecificDocumentOrDefaultAsync(DefaultLanguageCode);
new GetDocumentInput
{
ProjectId = Project.Id,
Name = DocumentNameWithExtension,
LanguageCode = DefaultLanguageCode,
Version = Version
}
);
DocumentLanguageIsDifferent = true; DocumentLanguageIsDifferent = true;
} }
@ -545,6 +515,33 @@ namespace Volo.Docs.Pages.Documents.Project
} }
private async Task<DocumentWithDetailsDto> GetSpecificDocumentOrDefaultAsync(string languageCode)
{
if (DocumentName.IsNullOrWhiteSpace())
{
return await _documentAppService.GetDefaultAsync(
new GetDefaultDocumentInput
{
ProjectId = Project.Id,
LanguageCode = languageCode,
Version = Version
}
);
}
else
{
return await _documentAppService.GetAsync(
new GetDocumentInput
{
ProjectId = Project.Id,
Name = DocumentNameWithExtension,
LanguageCode = languageCode,
Version = Version
}
);
}
}
public async Task SetDocumentPreferencesAsync() public async Task SetDocumentPreferencesAsync()
{ {
var projectParameters = await _documentAppService.GetParametersAsync( var projectParameters = await _documentAppService.GetParametersAsync(
@ -604,7 +601,7 @@ namespace Volo.Docs.Pages.Documents.Project
{ {
return null; return null;
} }
var firstParagraph = new Regex(@"<p>(.*?)</p>", RegexOptions.IgnoreCase); var firstParagraph = new Regex(@"<p>(.*?)</p>", RegexOptions.IgnoreCase);
var match = firstParagraph.Match(Document.Content); var match = firstParagraph.Match(Document.Content);
if (!match.Success) if (!match.Success)

21
modules/docs/src/Volo.Docs.Web/Pages/Documents/Shared/ErrorComponent/Default.cshtml

@ -3,14 +3,17 @@
} }
<div class="row position-relative vh-100" style="background: #e8e8e8;"> <div class="row position-relative vh-100" style="background: #e8e8e8;">
<div class="center" id="DocumentErrorContainer"> <div class="center" id="DocumentErrorContainer">
<span class="notfound-@Model.ErrorCode">@Model.ErrorCode</span> <span class="notfound-@Model.ErrorCode">@Model.ErrorCode</span>
<h1> <h1>
@Model.ErrorMessage @Model.ErrorMessage
</h1> </h1>
<br /> <br/>
<a href="@(Model.RedirectUrl)" class="btn btn-primary px-4" id="ErrorRedirect"> @if (Model.AutoRedirect)
Go Back <span id="ErrorRedirectSeconds">(3)</span> {
</a> <a href="@(Model.RedirectUrl)" class="btn btn-primary px-4" id="ErrorRedirect">
Go Back <span id="ErrorRedirectSeconds">(3)</span>
</a>
}
</div> </div>
</div> </div>
@ -19,7 +22,7 @@
h1 { h1 {
font-size: 1.75em; font-size: 1.75em;
line-height: 1.75; line-height: 1.75;
color: #777; color: #777;
font-weight: normal; font-weight: normal;
} }
@ -36,7 +39,7 @@
transform: translate(-50%, -50%); transform: translate(-50%, -50%);
text-align: center; text-align: center;
} }
.notfound-404 { .notfound-404 {
font-size: 300px; font-size: 300px;
font-weight: 700; font-weight: 700;
@ -48,5 +51,5 @@
margin-bottom: -150px; margin-bottom: -150px;
z-index: -1; z-index: -1;
position: relative; position: relative;
} }
</style> </style>

2
modules/docs/src/Volo.Docs.Web/Pages/Documents/Shared/ErrorComponent/ErrorPageModel.cs

@ -7,5 +7,7 @@
public string ErrorCode { get; set; } public string ErrorCode { get; set; }
public string RedirectUrl { get; set; } public string RedirectUrl { get; set; }
public bool AutoRedirect { get; set; } = true;
} }
} }

2
modules/docs/src/Volo.Docs.Web/Pages/Documents/Shared/ErrorComponent/error.js

@ -12,7 +12,7 @@
}, 1000); }, 1000);
} }
if (document.getElementById("DocumentErrorContainer")) { if (document.getElementById("ErrorRedirect")) {
errorPageRedirect(); errorPageRedirect();
} }
}); });

Loading…
Cancel
Save