-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement CacheReader and enhance asset handling with tests (#597)
* Implement CacheReader for efficient multiple reads and add unit tests * Refactor asset file handling to use OpenFile method and implement CacheReader for efficient data caching * Merge branch 'next' into refactor-temporary-files * Merge branch 'next' into refactor-temporary-files * Add end-to-end tests for archiving from Google Photos and Immich
- Loading branch information
Showing
11 changed files
with
349 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cachereader | ||
|
||
import ( | ||
"io" | ||
"os" | ||
) | ||
|
||
// CacheReader is a reader that caches the data in a temporary file to allow multiple reads | ||
type CacheReader struct { | ||
tmpFile *os.File // tmpFile is the temporary file or the original file | ||
shouldRemove bool | ||
} | ||
|
||
// NewCacheReader creates a new CacheReader from an io.ReadCloser | ||
// When the reader is an os.File, it will be used directly | ||
// Otherwise, the content will be copied into a temporary file, and the original reader will be closed | ||
func NewCacheReader(rc io.ReadCloser) (*CacheReader, error) { | ||
var err error | ||
c := &CacheReader{} | ||
if f, ok := rc.(*os.File); ok { | ||
c.tmpFile = f | ||
} else { | ||
c.tmpFile, err = os.CreateTemp("", "immich-go_*") | ||
if err != nil { | ||
return nil, err | ||
} | ||
// be sure to copy the reader content into the temporary file | ||
_, err = io.Copy(c.tmpFile, rc) | ||
rc.Close() | ||
c.shouldRemove = true | ||
} | ||
return c, err | ||
} | ||
|
||
// OpenFile creates a new file based on the temporary file | ||
func (cr *CacheReader) OpenFile() (*os.File, error) { | ||
f, err := os.Open(cr.tmpFile.Name()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return f, nil | ||
} | ||
|
||
// Close closes the temporary file only if it was created by NewCacheReader | ||
func (cr *CacheReader) Close() error { | ||
if cr.shouldRemove { | ||
// the source is already closed | ||
return os.Remove(cr.tmpFile.Name()) | ||
} else { | ||
return cr.tmpFile.Close() | ||
} | ||
} |
Oops, something went wrong.