-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import os | ||
import tempfile | ||
|
||
|
||
def get_fieldfile_local_path(fieldfile): | ||
local_temp_file = None | ||
|
||
if hasattr(fieldfile, 'storage'): | ||
storage = fieldfile.storage | ||
try: | ||
# Try to access with path | ||
storage_local_path = storage.path( | ||
fieldfile.path | ||
) | ||
except (NotImplementedError, AttributeError): | ||
# Storage doesnt support absolute paths, download | ||
# file to a temp local dir | ||
storage_file = storage.open( | ||
fieldfile.name, 'r' | ||
) | ||
|
||
local_temp_file = tempfile.NamedTemporaryFile( | ||
delete=False | ||
) | ||
local_temp_file.write(storage_file.read()) | ||
local_temp_file.seek(0) | ||
|
||
storage_local_path = local_temp_file.name | ||
else: | ||
# Its a local file with no storage abstraction | ||
try: | ||
path = os.path.abspath(fieldfile.path) | ||
except AttributeError: | ||
path = os.path.abspath(fieldfile.name) | ||
|
||
storage_local_path = path | ||
|
||
return storage_local_path, local_temp_file |