diff options
| author | rr- | 2017-08-24 13:42:45 +0200 |
|---|---|---|
| committer | rr- | 2017-08-24 17:17:09 +0200 |
| commit | 4afece8d50ccfa7dde6295bd6c2046eaa6334416 (patch) | |
| tree | 0cf7419886798b0ad57a16483cdf83e9dbafb27e /server/szurubooru/migrations | |
| parent | 90b0d771478a14e1d6d7fbbd9c6efaa28637535c (diff) | |
server/posts: add non-guessable IDs to post URLs
Diffstat (limited to 'server/szurubooru/migrations')
| -rw-r--r-- | server/szurubooru/migrations/versions/02ef5f73f4ab_add_hashes_to_post_file_names.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/server/szurubooru/migrations/versions/02ef5f73f4ab_add_hashes_to_post_file_names.py b/server/szurubooru/migrations/versions/02ef5f73f4ab_add_hashes_to_post_file_names.py new file mode 100644 index 0000000..c7e5d01 --- /dev/null +++ b/server/szurubooru/migrations/versions/02ef5f73f4ab_add_hashes_to_post_file_names.py @@ -0,0 +1,43 @@ +''' +Add hashes to post file names + +Revision ID: 02ef5f73f4ab +Created at: 2017-08-24 13:30:46.766928 +''' + +import os +import re +from szurubooru.func import files, posts + +revision = '02ef5f73f4ab' +down_revision = '5f00af3004a4' +branch_labels = None +depends_on = None + + +def upgrade(): + for name in ['posts', 'posts/custom-thumbnails', 'generated-thumbnails']: + for entry in list(files.scan(name)): + match = re.match(r'^(?P<name>\d+)\.(?P<ext>\w+)$', entry.name) + if match: + post_id = int(match.group('name')) + security_hash = posts.get_post_security_hash(post_id) + ext = match.group('ext') + new_name = '%s_%s.%s' % (post_id, security_hash, ext) + new_path = os.path.join(os.path.dirname(entry.path), new_name) + os.rename(entry.path, new_path) + + +def downgrade(): + for name in ['posts', 'posts/custom-thumbnails', 'generated-thumbnails']: + for entry in list(files.scan(name)): + match = re.match( + r'^(?P<name>\d+)_(?P<hash>[0-9A-Fa-f]+)\.(?P<ext>\w+)$', + entry.name) + if match: + post_id = int(match.group('name')) + security_hash = match.group('hash') + ext = match.group('ext') + new_name = '%s.%s' % (post_id, ext) + new_path = os.path.join(os.path.dirname(entry.path), new_name) + os.rename(entry.path, new_path) |