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