don't open files when stating directory (#797)

This commit is contained in:
Mike Sawka 2024-09-20 12:25:46 -07:00 committed by GitHub
parent e281cebb56
commit 31414a7536
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 5 deletions

View File

@ -629,7 +629,7 @@ func CopyToChannel(outputCh chan<- []byte, reader io.Reader) error {
// on error just returns ""
// does not return "application/octet-stream" as this is considered a detection failure
// can pass an existing fileInfo to avoid re-statting the file
func DetectMimeType(path string, fileInfo fs.FileInfo) string {
func DetectMimeType(path string, fileInfo fs.FileInfo, extended bool) string {
if fileInfo == nil {
statRtn, err := os.Stat(path)
if err != nil {
@ -657,6 +657,9 @@ func DetectMimeType(path string, fileInfo fs.FileInfo) string {
if mimeType := mime.TypeByExtension(ext); mimeType != "" {
return mimeType
}
if !extended {
return ""
}
fd, err := os.Open(path)
if err != nil {
return ""

View File

@ -104,7 +104,7 @@ func (impl *ServerImpl) remoteStreamFileDir(ctx context.Context, path string, by
if err != nil {
continue
}
innerFileInfo := statToFileInfo(filepath.Join(path, innerFileInfoInt.Name()), innerFileInfoInt)
innerFileInfo := statToFileInfo(filepath.Join(path, innerFileInfoInt.Name()), innerFileInfoInt, false)
fileInfoArr = append(fileInfoArr, innerFileInfo)
if len(fileInfoArr) >= DirChunkSize {
dataCallback(fileInfoArr, nil)
@ -200,8 +200,8 @@ func (impl *ServerImpl) RemoteStreamFileCommand(ctx context.Context, data wshrpc
return ch
}
func statToFileInfo(fullPath string, finfo fs.FileInfo) *wshrpc.FileInfo {
mimeType := utilfn.DetectMimeType(fullPath, finfo)
func statToFileInfo(fullPath string, finfo fs.FileInfo, extended bool) *wshrpc.FileInfo {
mimeType := utilfn.DetectMimeType(fullPath, finfo, extended)
rtn := &wshrpc.FileInfo{
Path: wavebase.ReplaceHomeDir(fullPath),
Dir: computeDirPart(fullPath, finfo.IsDir()),
@ -272,7 +272,7 @@ func (*ServerImpl) fileInfoInternal(path string, extended bool) (*wshrpc.FileInf
if err != nil {
return nil, fmt.Errorf("cannot stat file %q: %w", path, err)
}
rtn := statToFileInfo(cleanedPath, finfo)
rtn := statToFileInfo(cleanedPath, finfo, extended)
if extended {
rtn.ReadOnly = checkIsReadOnly(cleanedPath, finfo, true)
}