-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce FS.CompressRoot #1331
Conversation
fs.go
Outdated
if h.root == h.compressRoot { | ||
compressedFilePath = filePath | ||
} else { | ||
compressedFilePath = filepath.FromSlash(h.compressRoot + path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is filepath.FromSlash
needed here while it isn't for h.root
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. 5f432d9
fs.go
Outdated
@@ -1130,7 +1147,7 @@ const ( | |||
fsMaxCompressibleFileSize = 8 * 1024 * 1024 | |||
) | |||
|
|||
func (h *fsHandler) compressAndOpenFSFile(filePath string, fileEncoding string) (*fsFile, error) { | |||
func (h *fsHandler) compressAndOpenFSFile(path, filePath string, fileEncoding string) (*fsFile, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you come up wit ha better name for path
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which do you think better?
- pathStr
Line 833 in b23c5e9
pathStr := string(path) - pathOrig
- uri
fs.go
Outdated
@@ -1232,7 +1259,7 @@ func (h *fsHandler) newCompressedFSFile(filePath string, fileEncoding string) (* | |||
return h.newFSFile(f, fileInfo, true, fileEncoding) | |||
} | |||
|
|||
func (h *fsHandler) openFSFile(filePath string, mustCompress bool, fileEncoding string) (*fsFile, error) { | |||
func (h *fsHandler) openFSFile(path string, filePath string, mustCompress bool, fileEncoding string) (*fsFile, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you come up wit ha better name for path
here?
fs.go
Outdated
if h.root == h.compressRoot { | ||
compressedFilePath = filePath | ||
} else { | ||
compressedFilePath = h.compressRoot + path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it possible here instead to do something like if h.root != h.compressRoot { compressedFilePath = strings.Replace(filePath, h.root, h.compressedRoot, 1) }
? That way you don't have to pass the original path in each function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introduce pathToFilePath and filePathToCompressed 3dd25f3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found unnecessary changes, reverted them. fa6652b
89043cd
to
fa6652b
Compare
fs.go
Outdated
@@ -780,6 +796,20 @@ func cleanCacheNolock(cache map[string]*fsFile, pendingFiles, filesToRelease []* | |||
return pendingFiles, filesToRelease | |||
} | |||
|
|||
func (h *fsHandler) pathToFilePath(path string) string { | |||
return h.root + filepath.FromSlash(path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be filepath.FromSlash(h.root + path)
like it was in the code where you call this function now?
And then strings.HasPrefix(filePath, h.root)
should also use filepath.FromSlash(
I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Join root and path, and then calls filepath.FromSlash 38b41a8
root and compressRoot are passed through filepath.FromSlash in normalizeRoot. (eg. C:\path\to\root
)
The path is part of the URL, that is separated by slashes. (eg. /path/to/file
)
The path needs filepath.FromSlash to join root. (eg. C:\path\to\root
+ \path\to\file
)
I think the best way is to use "filepath.Join", but I used "+" and "filepath.FromSlash" to respect the original code.
func (h *fsHandler) pathToFilePath(path string) string {
return filepath.Join(h.root, path)
}
func (h *fsHandler) filePathToCompressed(filePath string) string {
if h.root == h.compressRoot {
return filePath
}
if !strings.HasPrefix(filePath, h.root) {
return filePath
}
return filepath.Join(h.compressRoot, filePath[len(h.root):])
}
Thanks! |
* Introduce FS.CompressRoot * Avoid duplicated filepath.FromSlash * Introduce filePathToCompressed * Revert openIndexFile manually * Join root and path, and then calls filepath.FromSlash
I want to serve files on read-only Root with FS.Compress.
This may be one of solution, could you review it?