Skip to content
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

Merged
merged 5 commits into from Jul 10, 2022
Merged

Conversation

mojatter
Copy link
Contributor

@mojatter mojatter commented Jul 1, 2022

I want to serve files on read-only Root with FS.Compress.

This may be one of solution, could you review it?

fs := &fasthttp.FS {
    Root: "/read-only/html",
    Compress: true,
    CompressRoot: "/tmp/compressed",
}

fs.go Outdated
if h.root == h.compressRoot {
compressedFilePath = filePath
} else {
compressedFilePath = filepath.FromSlash(h.compressRoot + path)
Copy link
Collaborator

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?

Copy link
Contributor Author

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) {
Copy link
Collaborator

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?

Copy link
Contributor Author

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?

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) {
Copy link
Collaborator

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
Copy link
Collaborator

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.

Copy link
Contributor Author

@mojatter mojatter Jul 7, 2022

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

Copy link
Contributor Author

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

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)
Copy link
Collaborator

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.

Copy link
Contributor Author

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):])
}

@erikdubbelboer erikdubbelboer merged commit f3513cc into valyala:master Jul 10, 2022
@erikdubbelboer
Copy link
Collaborator

Thanks!

bbenzikry pushed a commit to bbenzikry/fasthttp that referenced this pull request Sep 11, 2022
* Introduce FS.CompressRoot

* Avoid duplicated filepath.FromSlash

* Introduce filePathToCompressed

* Revert openIndexFile manually

* Join root and path, and then calls filepath.FromSlash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants