Resolved code formatting change requests
This commit is contained in:
parent
cb1f79ae98
commit
02a2e234e1
4 changed files with 41 additions and 44 deletions
|
@ -72,15 +72,18 @@ def create_post(
|
|||
create_snapshots_for_post(post, new_tags, None if anonymous else ctx.user)
|
||||
alternate_format_posts = posts.generate_alternate_formats(post, content)
|
||||
for alternate_post, alternate_post_new_tags in alternate_format_posts:
|
||||
create_snapshots_for_post(alternate_post,
|
||||
alternate_post_new_tags,
|
||||
None if anonymous else ctx.user)
|
||||
create_snapshots_for_post(
|
||||
alternate_post,
|
||||
alternate_post_new_tags,
|
||||
None if anonymous else ctx.user)
|
||||
ctx.session.commit()
|
||||
return _serialize_post(ctx, post)
|
||||
|
||||
|
||||
def create_snapshots_for_post(post: model.Post, new_tags: List[model.Tag],
|
||||
user: Optional[model.User]):
|
||||
def create_snapshots_for_post(
|
||||
post: model.Post,
|
||||
new_tags: List[model.Tag],
|
||||
user: Optional[model.User]):
|
||||
snapshots.create(post, user)
|
||||
for tag in new_tags:
|
||||
snapshots.create(tag, user)
|
||||
|
|
|
@ -134,9 +134,7 @@ class Image:
|
|||
]
|
||||
|
||||
if altered_dimensions:
|
||||
args = args + [
|
||||
'-filter:v', 'scale=\'%d:%d\'' % (width, height)
|
||||
]
|
||||
args += ['-filter:v', 'scale=\'%d:%d\'' % (width, height)]
|
||||
|
||||
self._execute(args + ['-y', mp4_temp_path])
|
||||
|
||||
|
|
|
@ -1,67 +1,58 @@
|
|||
import re
|
||||
from typing import Optional
|
||||
|
||||
_APPLICATION_OCTET_STREAM = 'application/octet-stream'
|
||||
_APPLICATION_OGG = 'application/ogg'
|
||||
_APPLICATION_SWF = 'application/x-shockwave-flash'
|
||||
_IMAGE_GIF = 'image/gif'
|
||||
_IMAGE_JPEG = 'image/jpeg'
|
||||
_IMAGE_PNG = 'image/png'
|
||||
_VIDEO_MP4 = 'video/mp4'
|
||||
_VIDEO_WEBM = 'video/webm'
|
||||
|
||||
|
||||
def get_mime_type(content: bytes) -> str:
|
||||
if not content:
|
||||
return _APPLICATION_OCTET_STREAM
|
||||
return 'application/octet-stream'
|
||||
|
||||
if content[0:3] in (b'CWS', b'FWS', b'ZWS'):
|
||||
return _APPLICATION_SWF
|
||||
return 'application/x-shockwave-flash'
|
||||
|
||||
if content[0:3] == b'\xFF\xD8\xFF':
|
||||
return _IMAGE_JPEG
|
||||
return 'image/jpeg'
|
||||
|
||||
if content[0:6] == b'\x89PNG\x0D\x0A':
|
||||
return _IMAGE_PNG
|
||||
return 'image/png'
|
||||
|
||||
if content[0:6] in (b'GIF87a', b'GIF89a'):
|
||||
return _IMAGE_GIF
|
||||
return 'image/gif'
|
||||
|
||||
if content[0:4] == b'\x1A\x45\xDF\xA3':
|
||||
return _VIDEO_WEBM
|
||||
return 'video/webm'
|
||||
|
||||
if content[4:12] in (b'ftypisom', b'ftypmp42'):
|
||||
return _VIDEO_MP4
|
||||
return 'video/mp4'
|
||||
|
||||
return _APPLICATION_OCTET_STREAM
|
||||
return 'application/octet-stream'
|
||||
|
||||
|
||||
def get_extension(mime_type: str) -> Optional[str]:
|
||||
extension_map = {
|
||||
_APPLICATION_SWF: 'swf',
|
||||
_IMAGE_GIF: 'gif',
|
||||
_IMAGE_JPEG: 'jpg',
|
||||
_IMAGE_PNG: 'png',
|
||||
_VIDEO_MP4: 'mp4',
|
||||
_VIDEO_WEBM: 'webm',
|
||||
_APPLICATION_OCTET_STREAM: 'dat',
|
||||
'application/x-shockwave-flash': 'swf',
|
||||
'image/gif': 'gif',
|
||||
'image/jpeg': 'jpg',
|
||||
'image/png': 'png',
|
||||
'video/mp4': 'mp4',
|
||||
'video/webm': 'webm',
|
||||
'application/octet-stream': 'dat',
|
||||
}
|
||||
return extension_map.get((mime_type or '').strip().lower(), None)
|
||||
|
||||
|
||||
def is_flash(mime_type: str) -> bool:
|
||||
return mime_type.lower() == _APPLICATION_SWF
|
||||
return mime_type.lower() == 'application/x-shockwave-flash'
|
||||
|
||||
|
||||
def is_video(mime_type: str) -> bool:
|
||||
return mime_type.lower() in (_APPLICATION_OGG, _VIDEO_MP4, _VIDEO_WEBM)
|
||||
return mime_type.lower() in ('application/ogg', 'video/mp4', 'video/webm')
|
||||
|
||||
|
||||
def is_image(mime_type: str) -> bool:
|
||||
return mime_type.lower() in (_IMAGE_JPEG, _IMAGE_PNG, _IMAGE_GIF)
|
||||
return mime_type.lower() in ('image/jpeg', 'image/png', 'image/gif')
|
||||
|
||||
|
||||
def is_animated_gif(content: bytes) -> bool:
|
||||
pattern = b'\x21\xF9\x04[\x00-\xFF]{4}\x00[\x2C\x21]'
|
||||
return (get_mime_type(content) == _IMAGE_GIF
|
||||
and len(re.findall(pattern, content)) > 1)
|
||||
return get_mime_type(content) == 'image/gif' \
|
||||
and len(re.findall(pattern, content)) > 1
|
||||
|
|
|
@ -435,20 +435,25 @@ def generate_alternate_formats(post: model.Post, content: bytes) \
|
|||
assert content
|
||||
new_posts = []
|
||||
if mime.is_animated_gif(content):
|
||||
tag_names = [tag_name.name for tag_name in
|
||||
[tag.names for tag in post.tags]]
|
||||
tag_names = [
|
||||
tag_name.name
|
||||
for tag_name in [tag.names for tag in post.tags]]
|
||||
|
||||
if config.config['convert']['gif']['to_mp4']:
|
||||
mp4_post, new_tags = create_post(images.Image(content).to_mp4(),
|
||||
tag_names, post.user)
|
||||
mp4_post, new_tags = create_post(
|
||||
images.Image(content).to_mp4(),
|
||||
tag_names,
|
||||
post.user)
|
||||
update_post_flags(mp4_post, ['loop'])
|
||||
update_post_safety(mp4_post, post.safety)
|
||||
update_post_source(mp4_post, post.source)
|
||||
new_posts += [(mp4_post, new_tags)]
|
||||
|
||||
if config.config['convert']['gif']['to_webm']:
|
||||
webm_post, new_tags = create_post(images.Image(content).to_webm(),
|
||||
tag_names, post.user)
|
||||
webm_post, new_tags = create_post(
|
||||
images.Image(content).to_webm(),
|
||||
tag_names,
|
||||
post.user)
|
||||
update_post_flags(webm_post, ['loop'])
|
||||
update_post_safety(webm_post, post.safety)
|
||||
update_post_source(webm_post, post.source)
|
||||
|
@ -456,7 +461,7 @@ def generate_alternate_formats(post: model.Post, content: bytes) \
|
|||
|
||||
db.session.flush()
|
||||
|
||||
new_posts = list(filter(lambda i: i[0] is not None, new_posts))
|
||||
new_posts = [p for p in new_posts if p[0] is not None]
|
||||
|
||||
new_relations = [p[0].post_id for p in new_posts]
|
||||
if len(new_relations) > 0:
|
||||
|
|
Reference in a new issue