Skip to content

Fix screenshot crash with long video title, #3334 #3469

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

Merged
merged 4 commits into from
Nov 8, 2022

Conversation

low-batt
Copy link
Contributor

@low-batt low-batt commented Aug 1, 2021

This commit will:

  • Change the method MPVController.handleEvent to raise an alert if
    screenshot generation failed
  • Change the method Utility.getLatestScreenshot to guard against an empty
    screenshot directory

This commit adds new text for the alert that will require localization.


Description:

NOTE the need for localization of the added alert message text.

@@ -664,6 +664,18 @@ class MPVController: NSObject {
case MPV_EVENT_COMMAND_REPLY:
let reply = event.pointee.reply_userdata
if reply == MPVController.UserData.screenshot {
let code = event.pointee.error
guard 0 <= code else {
Copy link
Member

Choose a reason for hiding this comment

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

I think I would prefer the condition to be flipped

Suggested change
guard 0 <= code else {
guard code >= 0 else {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@@ -664,6 +664,18 @@ class MPVController: NSObject {
case MPV_EVENT_COMMAND_REPLY:
let reply = event.pointee.reply_userdata
if reply == MPVController.UserData.screenshot {
let code = event.pointee.error
guard 0 <= code else {
let error = String.init(cString: mpv_error_string(code))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let error = String.init(cString: mpv_error_string(code))
let error = String(cString: mpv_error_string(code))

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 see my inexperience with Swift is showing. Fixed.

@@ -461,7 +461,7 @@ class Utility {
guard let contents = try? FileManager.default.contentsOfDirectory(
at: folder,
includingPropertiesForKeys: [.creationDateKey],
options: .skipsSubdirectoryDescendants) else { return nil }
options: .skipsSubdirectoryDescendants), !contents.isEmpty else { return nil }
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
options: .skipsSubdirectoryDescendants), !contents.isEmpty else { return nil }
options: .skipsSubdirectoryDescendants),
let latestFile = contents.first else { return nil }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. I did have to use var instead of let as the code below this updates latestFile.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see. I would probably replace the code below to use min(by:) with a custom comparator, to be honest, but it's up to you if you want to change that 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.

That was existing code that I thought could be simplified, but I chose to make the minimum change to fix the issue. I have just pushed a commit that simplifies this method. See if that is what you were expecting.

return latestFile
options: .skipsSubdirectoryDescendants),
!contents.isEmpty else { return nil }
return contents.max { a, b in a.creationDate! < b.creationDate! }
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return contents.max { a, b in a.creationDate! < b.creationDate! }
return contents.filter { $0.creationDate != nil }.max { $0.creationDate! < $1.creationDate! }

Or if you are sure that creationDate would never be nil, you can remove the filter clause.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Rebased with develop. Tested again. I kept the filter. Made me worry that someone could put the screen shot directory on some network device that doesn't provide it.

This commit will:

- Change the method  MPVController.handleEvent to raise an alert if
    screenshot generation failed
- Change the method  Utility.getLatestScreenshot to guard against an empty
    screenshot directory

This commit adds new text for the alert that will require localization.
This commit will address review comments on the changes to the methods
MPVController.handleEvent and Utility.getLatestScreenshot.
This commit will:
- Extend URL with a new optional computed property, creationDate
- Change the method Utility.getLatestScreenshot to use the array max method
  to find the latest screenshot file
Rebased with develop and addressed review comment.

This commit will:
- Extend URL with a new optional computed property, creationDate
- Change the method Utility.getLatestScreenshot to use the array max method
  to find the latest screenshot file
@uiryuu uiryuu merged commit 00c807e into iina:develop Nov 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash when screenshotting with long video title (No error display)
3 participants