Пример #1
0
def get_post_content_path(post: model.Post) -> str:
    assert post
    assert post.post_id
    return 'posts/%d_%s.%s' % (
        post.post_id,
        get_post_security_hash(post.post_id),
        mime.get_extension(post.mime_type) or 'dat')
Пример #2
0
 def _execute(
         self,
         cli: List[str],
         program: str = 'ffmpeg',
         ignore_error_if_data: bool = False) -> bytes:
     extension = mime.get_extension(mime.get_mime_type(self.content))
     assert extension
     with util.create_temp_file(suffix='.' + extension) as handle:
         handle.write(self.content)
         handle.flush()
         cli = [program, '-loglevel', '24'] + cli
         cli = [part.format(path=handle.name) for part in cli]
         proc = subprocess.Popen(
             cli,
             stdout=subprocess.PIPE,
             stdin=subprocess.PIPE,
             stderr=subprocess.PIPE)
         out, err = proc.communicate(input=self.content)
         if proc.returncode != 0:
             logger.warning(
                 'ffmpeg 명령어 실행 실패 (cli=%r, err=%r)',
                 ' '.join(shlex.quote(arg) for arg in cli),
                 err)
             if ((len(out) > 0 and not ignore_error_if_data)
                     or len(out) == 0):
                 raise errors.ProcessingError(
                     '이미지 처리 중 오류.\n'
                     + err.decode('utf-8'))
         return out
Пример #3
0
def get_post_content_url(post: model.Post) -> str:
    assert post
    return '%s/posts/%d_%s.%s' % (
        config.config['data_url'].rstrip('/'),
        post.post_id,
        get_post_security_hash(post.post_id),
        mime.get_extension(post.mime_type) or 'dat')
Пример #4
0
def get_post_content_path(post: model.Post) -> str:
    assert post
    assert post.post_id
    return 'posts/%d_%s.%s' % (
        post.post_id,
        get_post_security_hash(post.post_id),
        mime.get_extension(post.mime_type) or 'dat')
def upgrade():
    conn = op.get_bind()

    op.add_column("post", sa.Column("camera", sa.Text(), nullable=True))

    op.add_column("post", sa.Column("date_taken", sa.DateTime(),
                                    nullable=True))

    posts = sa.Table(
        "post",
        sa.MetaData(),
        sa.Column("id", sa.Integer, primary_key=True),
        sa.Column("camera", sa.Text, nullable=True),
        sa.Column("date_taken", sa.DateTime, nullable=True),
        sa.Column("mime-type", sa.Unicode(32), nullable=False),
        sa.Column("type", sa.Unicode(32), nullable=False),
    )

    for post in conn.execute(posts.select().where(posts.c.type != "flash")):
        ext = get_extension(post["mime-type"])
        filename = f"{post.id}_{get_post_security_hash(post.id)}.{ext}"

        content = open(_get_full_path("posts/" + filename), "rb").read()

        if post.type == "image":
            media = Image(content)
        else:
            media = Video(content)

        conn.execute(posts.update().where(posts.c.id == post.id).values(
            camera=media.camera,
            date_taken=media.date_taken,
        ))
Пример #6
0
def get_post_content_url(post: model.Post) -> str:
    assert post
    return '%s/posts/%d_%s.%s' % (
        config.config['data_url'].rstrip('/'),
        post.post_id,
        get_post_security_hash(post.post_id),
        mime.get_extension(post.mime_type) or 'dat')
Пример #7
0
 def _execute(
     self,
     cli: List[str],
     program: str = "ffmpeg",
     ignore_error_if_data: bool = False,
     get_logs: bool = False,
 ) -> bytes:
     extension = mime.get_extension(mime.get_mime_type(self.content))
     assert extension
     with util.create_temp_file(suffix="." + extension) as handle:
         handle.write(self.content)
         handle.flush()
         cli = [program, "-loglevel", "32" if get_logs else "24"] + cli
         cli = [part.format(path=handle.name) for part in cli]
         proc = subprocess.Popen(
             cli,
             stdout=subprocess.PIPE,
             stdin=subprocess.PIPE,
             stderr=subprocess.PIPE,
         )
         out, err = proc.communicate(input=self.content)
         if proc.returncode != 0:
             logger.warning(
                 "Failed to execute ffmpeg command (cli=%r, err=%r)",
                 " ".join(shlex.quote(arg) for arg in cli),
                 err,
             )
             if (len(out) > 0
                     and not ignore_error_if_data) or len(out) == 0:
                 raise errors.ProcessingError(
                     "Error while processing image.\n" +
                     err.decode("utf-8"))
         return err if get_logs else out
Пример #8
0
 def _execute(
         self,
         cli: List[str],
         program: str = 'ffmpeg',
         ignore_error_if_data: bool = False,
         get_logs: bool = False) -> bytes:
     extension = mime.get_extension(mime.get_mime_type(self.content))
     assert extension
     with util.create_temp_file(suffix='.' + extension) as handle:
         handle.write(self.content)
         handle.flush()
         cli = [program, '-loglevel', '32' if get_logs else '24'] + cli
         cli = [part.format(path=handle.name) for part in cli]
         proc = subprocess.Popen(
             cli,
             stdout=subprocess.PIPE,
             stdin=subprocess.PIPE,
             stderr=subprocess.PIPE)
         out, err = proc.communicate(input=self.content)
         if proc.returncode != 0:
             logger.warning(
                 'Failed to execute ffmpeg command (cli=%r, err=%r)',
                 ' '.join(shlex.quote(arg) for arg in cli),
                 err)
             if ((len(out) > 0 and not ignore_error_if_data)
                     or len(out) == 0):
                 raise errors.ProcessingError(
                     'Error while processing image.\n'
                     + err.decode('utf-8'))
         return err if get_logs else out
Пример #9
0
def get_post_content_url(post: model.Post) -> str:
    assert post
    return "%s/posts/%d_%s.%s" % (
        config.config["data_url"].rstrip("/"),
        post.post_id,
        get_post_security_hash(post.post_id),
        mime.get_extension(post.mime_type) or "dat",
    )
Пример #10
0
 def _execute(self, cli: List[str], program: str = 'ffmpeg') -> bytes:
     extension = mime.get_extension(mime.get_mime_type(self.content))
     assert extension
     with util.create_temp_file(suffix='.' + extension) as handle:
         handle.write(self.content)
         handle.flush()
         cli = [program, '-loglevel', '24'] + cli
         cli = [part.format(path=handle.name) for part in cli]
         proc = subprocess.Popen(cli,
                                 stdout=subprocess.PIPE,
                                 stdin=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
         out, err = proc.communicate(input=self.content)
         if proc.returncode != 0:
             logger.warning(
                 'Failed to execute ffmpeg command (cli=%r, err=%r)',
                 ' '.join(shlex.quote(arg) for arg in cli), err)
             raise errors.ProcessingError(
                 'Error while processing image.\n' + err.decode('utf-8'))
         return out
Пример #11
0
 def _execute(self, cli, program='ffmpeg'):
     extension = mime.get_extension(mime.get_mime_type(self.content))
     assert extension
     with util.create_temp_file(suffix='.' + extension) as handle:
         handle.write(self.content)
         handle.flush()
         cli = [program, '-loglevel', '24'] + cli
         cli = [part.format(path=handle.name) for part in cli]
         proc = subprocess.Popen(
             cli,
             stdout=subprocess.PIPE,
             stdin=subprocess.PIPE,
             stderr=subprocess.PIPE)
         out, err = proc.communicate(input=self.content)
         if proc.returncode != 0:
             logger.warning(
                 'Failed to execute ffmpeg command (cli=%r, err=%r)',
                 ' '.join(shlex.quote(arg) for arg in cli),
                 err)
             raise errors.ProcessingError(
                 'Error while processing image.\n' + err.decode('utf-8'))
         return out
Пример #12
0
def get_post_content_path(post: model.Post) -> str:
    assert post
    assert post.post_id
    return 'posts/%d.%s' % (post.post_id, mime.get_extension(post.mime_type)
                            or 'dat')
Пример #13
0
def test_get_extension(mime_type, expected_extension):
    assert mime.get_extension(mime_type) == expected_extension
Пример #14
0
def get_post_content_path(post):
    return 'posts/%d.%s' % (
        post.post_id, mime.get_extension(post.mime_type) or 'dat')
Пример #15
0
def get_post_content_url(post):
    return '%s/posts/%d.%s' % (
        config.config['data_url'].rstrip('/'),
        post.post_id,
        mime.get_extension(post.mime_type) or 'dat')
Пример #16
0
def test_get_extension(mime_type, expected_extension):
    assert mime.get_extension(mime_type) == expected_extension