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

fix: Issue #631 raise an error when trying to extract a file (.zip_) #636

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

WarrenU
Copy link

@WarrenU WarrenU commented Feb 20, 2025

Addresses issue #631 Unzipping a malformed/invalid zip file by raising an error if the extension is wrong.

  • Added isExensionExtractable function to check against file types we can extract from the xtractr package. (return bool if we can extract or not)
  • Removed internal unzip function, as xtractr can handle unzipping zip file.
  • example log: time=2025-02-21T10:53:00.459-08:00 level=ERROR msg="Error unexpected file extension type: .zip_" error="unsupported operation"

Trial screenshot below (No progress in bottom left as also mentioned in issue):
image

❓ This is my first PR and I'm a brand new beginner to superfile.
Where would I see these logged errors? I ran it with VScode and put some breakpoints in to ensure the code I was hitting was working as expected. And it made some __debug_bin3073481771.exe files. I'm running it on windows. But not sure how to introspect that file or where to see the errors.

…e) a file that does not have a valid extension
Copy link

netlify bot commented Feb 20, 2025

Deploy Preview for superfile canceled.

Name Link
🔨 Latest commit 2428670
🔍 Latest deploy log https://app.netlify.com/sites/superfile/deploys/67c04712d8b1f600080bc7bf

@jachewz
Copy link
Contributor

jachewz commented Feb 21, 2025

sort of related but is it a good idea to remove the ".zip" case since extractCompressFile() can handle".zip" files as well?

switch ext {
case ".zip":
err = unzip(panel.element[panel.cursor].location, outputDir)
if err != nil {
slog.Error("Error extract file", "error", err)
return
}
default:
err = extractCompressFile(panel.element[panel.cursor].location, outputDir)
if err != nil {
outPutLog("Error extract file", "error", err)
return
}
}
}

Reduces the need to maintain the in-house unzip() function.

@lazysegtree
Copy link
Collaborator

lazysegtree commented Feb 21, 2025

@WarrenU slog.Error goes to superfile log file whose location you can see by running spf path-list .

See - https://superfile.netlify.app/configure/config-file-path/
And set debug = true in config file to also see debug logs.

@@ -154,6 +156,22 @@ func isBufferPrintable(buffer []byte) bool {
return true
}

// isValidFileExtension checks if a string is a valid file extension.
// Returns nil if valid, otherwise returns an error with a descriptive message.
func isValidFileExtension(ext string) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should return a bool, not an error.
error denotes failures in execution. If you give an invalid extension to this function, it successfully detects that its invalid, and return false. There is no error.

@@ -519,6 +519,12 @@ func (m *model) extractFile() {
var err error
panel := &m.fileModel.filePanels[m.filePanelFocusIndex]
ext := strings.ToLower(filepath.Ext(panel.element[panel.cursor].location))
err = isValidFileExtension(ext)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We dont wanna check if the extension is valid, but if it can be extracted or not. I dont think .jpg can be extracted. We should replace this with isExensionExtractable and just check for a few supported extenstions .zip, .tar.gz, .rar whichever we support.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to isExensionExtractable that makes sense and I agree.

@lazysegtree
Copy link
Collaborator

sort of related but is it a good idea to remove the ".zip" case since extractCompressFile() can handle".zip" files as well?

@jachewz If it can handle, then we should remove it. Then we dont need the unzip function and we can delete that .

@WarrenU
Copy link
Author

WarrenU commented Feb 21, 2025

sort of related but is it a good idea to remove the ".zip" case since extractCompressFile() can handle".zip" files as well?

@jachewz If it can handle, then we should remove it. Then we dont need the unzip function and we can delete that .

I removed the unzip, and opted to use xtractr default. I also added an explicit slog call for any error it raises.

Good mention @jachewz

@lazysegtree
Copy link
Collaborator

lazysegtree commented Feb 21, 2025

I would like us a to do a bit of testing that it can handle .tar.gz , .rar and most of these extensions. I will try to test it out sometimes. It would be great if @WarrenU you could also perform some of the tests.

@WarrenU
Copy link
Author

WarrenU commented Feb 25, 2025

.rar, .tar.gz, .zip are working. ✅

@yorukot
Copy link
Owner

yorukot commented Feb 27, 2025

I just discovered during testing that if there is a directory in the decompression, permission denied will occur, so I added:

x := &xtractr.XFile{
	FilePath:  src,
	OutputDir: dest,
+	FileMode:  0644,
+	DirMode:   0755,
}

Now it seems that it can be merge.

Btw The reason why I separated the zip is actually because I want to allow users to see the real-time progress.But it is fine to remove it.

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.

4 participants