summaryrefslogtreecommitdiff
path: root/check/commands/imagehash/add.py
blob: 73f8b693c27f704dd576ea2114f140e3cfd4765e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
Add a file to the database
"""

import click
import os

from app.models.sql_factory import search_by_phash, add_phash
from app.utils.im_utils import compute_phash_int
from app.utils.file_utils import sha256

@click.command()
@click.option('-i', '--input', 'opt_fn',
  required=True,
  help="File to add (gif/jpg/png)")
@click.option('-u', '--upload', 'opt_upload', is_flag=True,
  help='Whether to upload this file to S3')
@click.pass_context
def cli(ctx, opt_fn, opt_upload):
  """
  Add a single file
  """
  print('Adding a file...')
  if not os.path.exists(opt_fn):
    print("File does not exist")
    return

  hash = sha256(opt_fn)
  phash = compute_phash_int(opt_fn)

  dir, fn = os.path.split(opt_fn)
  root, ext = os.path.splitext(fn)

  add_phash(sha256=hash, phash=phash, ext=ext)