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

Add local file persister #1195

Closed
wants to merge 7 commits into from
Closed

Conversation

ankur22
Copy link
Collaborator

@ankur22 ankur22 commented Jan 25, 2024

What?

This adds a local file persister which will save files to the local disk. It implements the interface defined in #1155.

Why?

So that we can replace how we're currently writing screenshots to disk. This will help hide the details of where and how the files are saved, allowing us to easily work with different file persisters.

Checklist

  • I have performed a self-review of my code
  • I have added tests for my changes
  • I have commented on my code, particularly in hard-to-understand areas

Related PR(s)/Issue(s)

Updates: #1156

@ankur22 ankur22 marked this pull request as draft January 25, 2024 18:57
@ankur22 ankur22 changed the base branch from main to main-next January 25, 2024 18:58
@ankur22 ankur22 force-pushed the add/local-file-persister branch 2 times, most recently from 3dc6b6f to b797637 Compare January 29, 2024 10:53
This interface is what the local persister and remote persister will
have to conform to. It will allow the browser module to easily abstract
away the detail of where and how the file is being persisted.
The file persister will either be a local one or a remote one. The
decision will be made when the moduleVU is created, and one will be
initialised and set in the moduleVU instance.
@ankur22 ankur22 force-pushed the add/local-file-persister branch from b797637 to a1811ba Compare January 29, 2024 11:55
@ankur22 ankur22 changed the base branch from main-next to main January 29, 2024 11:55
@ankur22 ankur22 force-pushed the add/local-file-persister branch 4 times, most recently from f814d97 to 7a72b00 Compare January 29, 2024 12:40
@ankur22 ankur22 changed the base branch from main to add/file-persister January 29, 2024 12:57
@ankur22 ankur22 force-pushed the add/local-file-persister branch from 7a72b00 to 991e511 Compare January 29, 2024 12:59
@ankur22 ankur22 requested a review from inancgumus January 29, 2024 13:00
@ankur22 ankur22 marked this pull request as ready for review January 29, 2024 13:00
func write(w io.Writer, r io.Reader) error {
buf := make([]byte, 4096)
for {
n, err := r.Read(buf)
if n > 0 {
if _, writeErr := w.Write(buf[:n]); writeErr != nil {
return fmt.Errorf("writing to the local writer: %w", writeErr)
}
}
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("reading from the reader: %w", err)
}
}

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.

Why don't we just use io.Copy here?

Copy link
Collaborator Author

@ankur22 ankur22 Jan 29, 2024

Choose a reason for hiding this comment

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

I have no idea how i missed that 😆. Good suggestion, added to 7c39fb9

}
defer func() {
if err := f.Close(); err != nil {
logger.Errorf("LocalFilePersister:Persist", "closing the local file %q: %v", cp, err)
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 we can handle this also from the call site if we return an error here. So we don't have to pass a logger here. The call site can log the error if it sees it's a file close error. WDYT?

Copy link
Collaborator Author

@ankur22 ankur22 Jan 29, 2024

Choose a reason for hiding this comment

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

Yeah, that's a nice idea! Added it to 9476537

In which case the logger can be removed and has been 🎉

@ankur22 ankur22 force-pushed the add/local-file-persister branch from cfa2726 to 80acf99 Compare January 29, 2024 15:46
This is the file persister that will be used when testing locally. This
will replace the current implementation that is in screenshotter.
@ankur22 ankur22 force-pushed the add/local-file-persister branch from 80acf99 to 0c350c1 Compare January 29, 2024 15:51
Return an error to the caller if the close errors, but only if there
isn't an existing error. This helps removes the logger that is no
longer needed.
@ankur22 ankur22 force-pushed the add/local-file-persister branch from b4b354e to 9476537 Compare January 29, 2024 15:57
@ankur22 ankur22 force-pushed the add/file-persister branch from de5f6be to f35a27a Compare January 30, 2024 11:21
@ankur22
Copy link
Collaborator Author

ankur22 commented Jan 30, 2024

Closing this and moving this to #1197

@ankur22 ankur22 closed this Jan 30, 2024
@ankur22 ankur22 deleted the add/local-file-persister branch January 30, 2024 11:25
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.

2 participants