server/tools: add tool to change post filenames due to changed secret
This commit is contained in:
parent
83442b4977
commit
369ddaf2f8
1 changed files with 48 additions and 9 deletions
|
@ -5,13 +5,16 @@ Collection of CLI commands for an administrator to use
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
import re
|
||||||
import time
|
import time
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from getpass import getpass
|
from getpass import getpass
|
||||||
from sys import stderr
|
from sys import stderr
|
||||||
|
|
||||||
from szurubooru import db, errors, model
|
from szurubooru import config, db, errors, model
|
||||||
from szurubooru.func import files, images, posts as postfuncs, users as userfuncs
|
from szurubooru.func import files, images, \
|
||||||
|
posts as postfuncs, users as userfuncs
|
||||||
|
|
||||||
|
|
||||||
def reset_password(username: str) -> None:
|
def reset_password(username: str) -> None:
|
||||||
|
@ -30,10 +33,10 @@ def reset_password(username: str) -> None:
|
||||||
|
|
||||||
def check_audio() -> None:
|
def check_audio() -> None:
|
||||||
post_list = (db.session
|
post_list = (db.session
|
||||||
.query(model.Post)
|
.query(model.Post)
|
||||||
.filter(model.Post.type == model.Post.TYPE_VIDEO)
|
.filter(model.Post.type == model.Post.TYPE_VIDEO)
|
||||||
.order_by(model.Post.post_id)
|
.order_by(model.Post.post_id)
|
||||||
.all())
|
.all())
|
||||||
|
|
||||||
for post in post_list:
|
for post in post_list:
|
||||||
print('Checking post %d ...' % post.post_id, end='\r')
|
print('Checking post %d ...' % post.post_id, end='\r')
|
||||||
|
@ -43,7 +46,8 @@ def check_audio() -> None:
|
||||||
try:
|
try:
|
||||||
has_sound_data = images.Image(content).check_for_sound()
|
has_sound_data = images.Image(content).check_for_sound()
|
||||||
except errors.ProcessingError:
|
except errors.ProcessingError:
|
||||||
print('Post %d caused an error when checking for sound' % post.post_id)
|
print('Post %d caused an error when checking for sound' %
|
||||||
|
post.post_id)
|
||||||
|
|
||||||
if has_sound_data and not has_existing_flag:
|
if has_sound_data and not has_existing_flag:
|
||||||
print('Post %d has sound data but is not flagged' % post.post_id)
|
print('Post %d has sound data but is not flagged' % post.post_id)
|
||||||
|
@ -51,15 +55,48 @@ def check_audio() -> None:
|
||||||
print('Post %d has no sound data but is flagged' % post.post_id)
|
print('Post %d has no sound data but is flagged' % post.post_id)
|
||||||
|
|
||||||
|
|
||||||
|
def reset_filenames() -> None:
|
||||||
|
regex = re.compile(r'(\d+)_[0-9a-f]{16}\.(\S+)')
|
||||||
|
|
||||||
|
def convert_to_new_filename(old_name: str) -> str:
|
||||||
|
matches = regex.match(old_name)
|
||||||
|
if not matches:
|
||||||
|
raise ValueError('Not a valid filename')
|
||||||
|
post_id = int(matches.group(1))
|
||||||
|
post_ext = matches.group(2)
|
||||||
|
return '%d_%s.%s' % \
|
||||||
|
(post_id, postfuncs.get_post_security_hash(post_id), post_ext)
|
||||||
|
|
||||||
|
def rename_in_dir(dir: str) -> None:
|
||||||
|
for file in os.listdir(config.config['data_dir'] + dir):
|
||||||
|
try:
|
||||||
|
old_path = file
|
||||||
|
new_path = convert_to_new_filename(file)
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
if old_path != new_path:
|
||||||
|
print('%s -> %s' % (dir + old_path, dir + new_path))
|
||||||
|
os.rename(config.config['data_dir'] + dir + old_path,
|
||||||
|
config.config['data_dir'] + dir + new_path)
|
||||||
|
|
||||||
|
rename_in_dir('posts/')
|
||||||
|
rename_in_dir('generated-thumbnails/')
|
||||||
|
rename_in_dir('posts/custom-thumbnails/')
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
parser_top = ArgumentParser(
|
parser_top = ArgumentParser(
|
||||||
description='Collection of CLI commands for an administrator to use',
|
description='Collection of CLI commands for an administrator to use',
|
||||||
epilog='Look at README.md for more info')
|
epilog='Look at README.md for more info')
|
||||||
parser = parser_top.add_mutually_exclusive_group(required=True)
|
parser = parser_top.add_mutually_exclusive_group(required=True)
|
||||||
parser.add_argument('--change-password', metavar='<username>',
|
parser.add_argument('--change-password', metavar='<username>',
|
||||||
help='change the password of specified user')
|
help='change the password of specified user')
|
||||||
parser.add_argument('--check-all-audio', action='store_true',
|
parser.add_argument('--check-all-audio', action='store_true',
|
||||||
help='check the audio flags of all posts, noting discrepancies, without modifying posts')
|
help='check the audio flags of all posts, '
|
||||||
|
'noting discrepancies, without modifying posts')
|
||||||
|
parser.add_argument('--reset-filenames', action='store_true',
|
||||||
|
help='reset the content and thumbnail filenames'
|
||||||
|
'caused by a lost/changed secret key')
|
||||||
command = parser_top.parse_args()
|
command = parser_top.parse_args()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -67,6 +104,8 @@ def main() -> None:
|
||||||
reset_password(command.change_password)
|
reset_password(command.change_password)
|
||||||
elif command.check_all_audio:
|
elif command.check_all_audio:
|
||||||
check_audio()
|
check_audio()
|
||||||
|
elif command.reset_filenames:
|
||||||
|
reset_filenames()
|
||||||
except errors.BaseError as e:
|
except errors.BaseError as e:
|
||||||
print(e, file=stderr)
|
print(e, file=stderr)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue