This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Basic implementation of backup media store #2538
Merged
Merged
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
bf4fb1f
Basic implementation of backup media store
erikjohnston 67cb89f
Fix typo
erikjohnston c8eeef6
Fix typos
erikjohnston 6dfde6d
Remove dead code
erikjohnston b77a138
Typo
erikjohnston e283b55
Copy everything to backup
erikjohnston 802ca12
Don't close file prematurely
erikjohnston 1259a76
Get len before close
erikjohnston cc505b4
getvalue closes buffer
erikjohnston 4ae85ae
Don't close prematurely..
erikjohnston d76621a
Fix comments
erikjohnston b60859d
Use make_deferred_yieldable
erikjohnston 64db043
Move makedirs to thread
erikjohnston 3533229
Fix up comments
erikjohnston e3428d2
Fix typo
erikjohnston 5053714
Fix up thumbnailing function
erikjohnston 0e28281
Fix up
erikjohnston 9732ec6
s/write_to_file/write_to_file_and_backup/
erikjohnston ae5d186
Make things be absolute paths again
erikjohnston 4d7e1dd
Remove unnecessary diff
erikjohnston a675bd0
Add paths back in...
erikjohnston 1f43d22
Don't needlessly rename variable
erikjohnston c021c39
Remove spurious addition
erikjohnston ad1911b
Comment
erikjohnston 31aa7bd
Move type into key
erikjohnston b92a8e6
PEP8
erikjohnston 2b24416
Don't reuse source but instead copy from primary media store to backup
erikjohnston 6b725cf
Remove old comment
erikjohnston 1b6b0b1
Add try/finally block to close t_byte_source
erikjohnston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,80 +15,111 @@ | |
|
||
import os | ||
import re | ||
import functools | ||
|
||
NEW_FORMAT_ID_RE = re.compile(r"^\d\d\d\d-\d\d-\d\d") | ||
|
||
|
||
def _wrap_in_base_path(func): | ||
"""Takes a function that returns a relative path and turns it into an | ||
absolute path based on the location of the primary media store | ||
""" | ||
@functools.wraps(func) | ||
def _wrapped(self, *args, **kwargs): | ||
path = func(self, *args, **kwargs) | ||
return os.path.join(self.base_path, path) | ||
|
||
return _wrapped | ||
|
||
|
||
class MediaFilePaths(object): | ||
"""Describes where files are stored on disk. | ||
|
||
Most of the functions have a `*_rel` variant which returns a file path that | ||
is relative to the base media store path. This is mainly used when we want | ||
to write to the backup media store (when one is configured) | ||
""" | ||
|
||
def __init__(self, base_path): | ||
self.base_path = base_path | ||
def __init__(self, primary_base_path): | ||
self.base_path = primary_base_path | ||
|
||
def default_thumbnail(self, default_top_level, default_sub_type, width, | ||
height, content_type, method): | ||
def default_thumbnail_rel(self, default_top_level, default_sub_type, width, | ||
height, content_type, method): | ||
top_level_type, sub_type = content_type.split("/") | ||
file_name = "%i-%i-%s-%s-%s" % ( | ||
width, height, top_level_type, sub_type, method | ||
) | ||
return os.path.join( | ||
self.base_path, "default_thumbnails", default_top_level, | ||
"default_thumbnails", default_top_level, | ||
default_sub_type, file_name | ||
) | ||
|
||
def local_media_filepath(self, media_id): | ||
default_thumbnail = _wrap_in_base_path(default_thumbnail_rel) | ||
|
||
def local_media_filepath_rel(self, media_id): | ||
return os.path.join( | ||
self.base_path, "local_content", | ||
"local_content", | ||
media_id[0:2], media_id[2:4], media_id[4:] | ||
) | ||
|
||
def local_media_thumbnail(self, media_id, width, height, content_type, | ||
method): | ||
local_media_filepath = _wrap_in_base_path(local_media_filepath_rel) | ||
|
||
def local_media_thumbnail_rel(self, media_id, width, height, content_type, | ||
method): | ||
top_level_type, sub_type = content_type.split("/") | ||
file_name = "%i-%i-%s-%s-%s" % ( | ||
width, height, top_level_type, sub_type, method | ||
) | ||
return os.path.join( | ||
self.base_path, "local_thumbnails", | ||
"local_thumbnails", | ||
media_id[0:2], media_id[2:4], media_id[4:], | ||
file_name | ||
) | ||
|
||
def remote_media_filepath(self, server_name, file_id): | ||
local_media_thumbnail = _wrap_in_base_path(local_media_thumbnail_rel) | ||
|
||
def remote_media_filepath_rel(self, server_name, file_id): | ||
return os.path.join( | ||
self.base_path, "remote_content", server_name, | ||
"remote_content", server_name, | ||
file_id[0:2], file_id[2:4], file_id[4:] | ||
) | ||
|
||
def remote_media_thumbnail(self, server_name, file_id, width, height, | ||
content_type, method): | ||
remote_media_filepath = _wrap_in_base_path(remote_media_filepath_rel) | ||
|
||
def remote_media_thumbnail_rel(self, server_name, file_id, width, height, | ||
content_type, method): | ||
top_level_type, sub_type = content_type.split("/") | ||
file_name = "%i-%i-%s-%s" % (width, height, top_level_type, sub_type) | ||
return os.path.join( | ||
self.base_path, "remote_thumbnail", server_name, | ||
"remote_thumbnail", server_name, | ||
file_id[0:2], file_id[2:4], file_id[4:], | ||
file_name | ||
) | ||
|
||
remote_media_thumbnail = _wrap_in_base_path(remote_media_thumbnail_rel) | ||
|
||
def remote_media_thumbnail_dir(self, server_name, file_id): | ||
return os.path.join( | ||
self.base_path, "remote_thumbnail", server_name, | ||
file_id[0:2], file_id[2:4], file_id[4:], | ||
) | ||
|
||
def url_cache_filepath(self, media_id): | ||
def url_cache_filepath_rel(self, media_id): | ||
if NEW_FORMAT_ID_RE.match(media_id): | ||
# Media id is of the form <DATE><RANDOM_STRING> | ||
# E.g.: 2017-09-28-fsdRDt24DS234dsf | ||
return os.path.join( | ||
self.base_path, "url_cache", | ||
"url_cache", | ||
media_id[:10], media_id[11:] | ||
) | ||
else: | ||
return os.path.join( | ||
self.base_path, "url_cache", | ||
"url_cache", | ||
media_id[0:2], media_id[2:4], media_id[4:], | ||
) | ||
|
||
url_cache_filepath = _wrap_in_base_path(url_cache_filepath_rel) | ||
|
||
def url_cache_filepath_dirs_to_delete(self, media_id): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This actually returns absolute paths (now..) |
||
"The dirs to try and remove if we delete the media_id file" | ||
if NEW_FORMAT_ID_RE.match(media_id): | ||
|
@@ -110,8 +141,8 @@ def url_cache_filepath_dirs_to_delete(self, media_id): | |
), | ||
] | ||
|
||
def url_cache_thumbnail(self, media_id, width, height, content_type, | ||
method): | ||
def url_cache_thumbnail_rel(self, media_id, width, height, content_type, | ||
method): | ||
# Media id is of the form <DATE><RANDOM_STRING> | ||
# E.g.: 2017-09-28-fsdRDt24DS234dsf | ||
|
||
|
@@ -122,17 +153,19 @@ def url_cache_thumbnail(self, media_id, width, height, content_type, | |
|
||
if NEW_FORMAT_ID_RE.match(media_id): | ||
return os.path.join( | ||
self.base_path, "url_cache_thumbnails", | ||
"url_cache_thumbnails", | ||
media_id[:10], media_id[11:], | ||
file_name | ||
) | ||
else: | ||
return os.path.join( | ||
self.base_path, "url_cache_thumbnails", | ||
"url_cache_thumbnails", | ||
media_id[0:2], media_id[2:4], media_id[4:], | ||
file_name | ||
) | ||
|
||
url_cache_thumbnail = _wrap_in_base_path(url_cache_thumbnail_rel) | ||
|
||
def url_cache_thumbnail_directory(self, media_id): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This actually returns absolute paths (now..) |
||
# Media id is of the form <DATE><RANDOM_STRING> | ||
# E.g.: 2017-09-28-fsdRDt24DS234dsf | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this get a
_rel
for consistency?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This actually returns absolute paths (now..)