0
0
mirror of https://dev.sigpipe.me/dashie/git.txt synced 2025-10-06 02:12:39 +02:00

Fix handling of files within a subdirectory

This commit is contained in:
Dashie
2018-03-08 14:57:22 +01:00
parent 16819cd115
commit e3640aeb5f
4 changed files with 11 additions and 3 deletions

View File

@@ -8,7 +8,7 @@
* Ways of improvement
** TODO Merge AssignUser and AssignRepository ?
** TODO Cleanup gogs imports of unused bits
** TODO Can't add files with filename "/some/thing/foo.bar"
** DONE Can't add files with filename "/some/thing/foo.bar"
** DONE Investigate the non-working context.Flash when not using a redirect
** DONE Correctly handle filenames with a /, like raw view
** DONE strip '/' from filenames

View File

@@ -58,6 +58,7 @@ func NewPost(ctx *context.Context, f form.Gitxt) {
for i := range f.FilesFilename {
// For each filename sanitize it
f.FilesFilename[i] = sanitize.Filename(f.FilesFilename[i])
f.FilesFilename[i] = sanitize.RemoveFirstSlash(f.FilesFilename[i])
if len(f.FilesFilename[i]) == 0 || f.FilesFilename[i] == "." {
// If length is zero, use default filename
f.FilesFilename[i] = fmt.Sprintf("gitxt%d.txt", i)

View File

@@ -133,7 +133,9 @@ func cleanString(s string, r *regexp.Regexp) string {
}
// We are very restrictive as this could be intended for ascii url slugs
var illegalPath = regexp.MustCompile(`[^[:alnum:]\~\-\_\./]`)
var illegalPath = regexp.MustCompile(`[^[:alnum:]\~\-\_\.]/`)
var slashes = regexp.MustCompile(`^[/]+`)
// Filename will clean illegal characters
func Filename(s string) string {
@@ -146,3 +148,8 @@ func Filename(s string) string {
// NB this may be of length 0, caller must check
return filePath
}
// RemoveFirstSlash remove the firstS slashes of filename
func RemoveFirstSlash(s string) string {
return slashes.ReplaceAllString(s, "")
}

View File

@@ -55,7 +55,7 @@
{{range $entryFile := .repoFiles}}
<div class="gitxt_file">
<div class="header clearfix" id="file_{{$entryFile.Path}}">
<div class="left"><i class="fa fa-file-code-o" aria-hidden="true"></i> <a href="#file_{{$entryFile.Path}}">{{$entryFile.Path}}</a> <span class="file_size">{{FileSize $entryFile.Size}}</span> <small>({{$entryFile.MimeType}})</small></div>
<div class="left"><i class="fa fa-file-code-o" aria-hidden="true"></i> <a href="#file_{{$entryFile.Path}}">/{{$entryFile.Path}}</a> <span class="file_size">{{FileSize $entryFile.Size}}</span> <small>({{$entryFile.MimeType}})</small></div>
<div class="right"><a class="btn btn-xs btn-default" href="{{AppSubURL}}/{{$.repoOwnerUsername}}/{{$.repoHash}}/raw/{{$entryFile.Path}}">{{$.i18n.Tr "gitxt_view.raw_file"}}</a></div>
</div>
<div class="gitxt_content">