Refactor readme file renderer (#19502)
* Refactor readme file renderer * improve
This commit is contained in:
		
							parent
							
								
									e4274f640c
								
							
						
					
					
						commit
						d71df01077
					
				| 
						 | 
				
			
			@ -146,6 +146,21 @@ func renderDirectory(ctx *context.Context, treeLink string) {
 | 
			
		|||
		ctx.Data["Title"] = ctx.Tr("repo.file.title", ctx.Repo.Repository.Name+"/"+path.Base(ctx.Repo.TreePath), ctx.Repo.RefName)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Check permission to add or upload new file.
 | 
			
		||||
	if ctx.Repo.CanWrite(unit_model.TypeCode) && ctx.Repo.IsViewBranch {
 | 
			
		||||
		ctx.Data["CanAddFile"] = !ctx.Repo.Repository.IsArchived
 | 
			
		||||
		ctx.Data["CanUploadFile"] = setting.Repository.Upload.Enabled && !ctx.Repo.Repository.IsArchived
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	readmeFile, readmeTreelink := findReadmeFile(ctx, entries, treeLink)
 | 
			
		||||
	if ctx.Written() || readmeFile == nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	renderReadmeFile(ctx, readmeFile, readmeTreelink)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func findReadmeFile(ctx *context.Context, entries git.Entries, treeLink string) (*namedBlob, string) {
 | 
			
		||||
	// 3 for the extensions in exts[] in order
 | 
			
		||||
	// the last one is for a readme that doesn't
 | 
			
		||||
	// strictly match an extension
 | 
			
		||||
| 
						 | 
				
			
			@ -183,7 +198,7 @@ func renderDirectory(ctx *context.Context, treeLink string) {
 | 
			
		|||
					target, err = entry.FollowLinks()
 | 
			
		||||
					if err != nil && !git.IsErrBadLink(err) {
 | 
			
		||||
						ctx.ServerError("FollowLinks", err)
 | 
			
		||||
						return
 | 
			
		||||
						return nil, ""
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				log.Debug("%t", target == nil)
 | 
			
		||||
| 
						 | 
				
			
			@ -205,7 +220,7 @@ func renderDirectory(ctx *context.Context, treeLink string) {
 | 
			
		|||
				entry, err = entry.FollowLinks()
 | 
			
		||||
				if err != nil && !git.IsErrBadLink(err) {
 | 
			
		||||
					ctx.ServerError("FollowLinks", err)
 | 
			
		||||
					return
 | 
			
		||||
					return nil, ""
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			if entry != nil && (entry.IsExecutable() || entry.IsRegular()) {
 | 
			
		||||
| 
						 | 
				
			
			@ -236,7 +251,7 @@ func renderDirectory(ctx *context.Context, treeLink string) {
 | 
			
		|||
			readmeFile, err = getReadmeFileFromPath(ctx.Repo.Commit, entry.GetSubJumpablePathName())
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				ctx.ServerError("getReadmeFileFromPath", err)
 | 
			
		||||
				return
 | 
			
		||||
				return nil, ""
 | 
			
		||||
			}
 | 
			
		||||
			if readmeFile != nil {
 | 
			
		||||
				readmeFile.name = entry.Name() + "/" + readmeFile.name
 | 
			
		||||
| 
						 | 
				
			
			@ -245,8 +260,10 @@ func renderDirectory(ctx *context.Context, treeLink string) {
 | 
			
		|||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return readmeFile, readmeTreelink
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
	if readmeFile != nil {
 | 
			
		||||
func renderReadmeFile(ctx *context.Context, readmeFile *namedBlob, readmeTreelink string) {
 | 
			
		||||
	ctx.Data["RawFileLink"] = ""
 | 
			
		||||
	ctx.Data["ReadmeInList"] = true
 | 
			
		||||
	ctx.Data["ReadmeExist"] = true
 | 
			
		||||
| 
						 | 
				
			
			@ -314,17 +331,22 @@ func renderDirectory(ctx *context.Context, treeLink string) {
 | 
			
		|||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if !isTextFile {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if !isLFSFile {
 | 
			
		||||
		fileSize = readmeFile.blob.Size()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
		if isTextFile {
 | 
			
		||||
	if fileSize >= setting.UI.MaxDisplayFileSize {
 | 
			
		||||
		// Pretend that this is a normal text file to display 'This file is too large to be shown'
 | 
			
		||||
		ctx.Data["IsFileTooLarge"] = true
 | 
			
		||||
		ctx.Data["IsTextFile"] = true
 | 
			
		||||
		ctx.Data["FileSize"] = fileSize
 | 
			
		||||
			} else {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc))
 | 
			
		||||
 | 
			
		||||
	if markupType := markup.Type(readmeFile.name); markupType != "" {
 | 
			
		||||
| 
						 | 
				
			
			@ -361,15 +383,6 @@ func renderDirectory(ctx *context.Context, treeLink string) {
 | 
			
		|||
		)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Check permission to add or upload new file.
 | 
			
		||||
	if ctx.Repo.CanWrite(unit_model.TypeCode) && ctx.Repo.IsViewBranch {
 | 
			
		||||
		ctx.Data["CanAddFile"] = !ctx.Repo.Repository.IsArchived
 | 
			
		||||
		ctx.Data["CanUploadFile"] = setting.Repository.Upload.Enabled && !ctx.Repo.Repository.IsArchived
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink string) {
 | 
			
		||||
	ctx.Data["IsViewFile"] = true
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue