Skip to content

Commit

Permalink
fixed missing project param & SQL injection
Browse files Browse the repository at this point in the history
  • Loading branch information
bkellenb committed May 14, 2021
1 parent de150d0 commit 18efa4c
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions projectCreation/import_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@
*.ini file).
3. Call the script from the AIDE code base on the FileServer instance.
2019-20 Benjamin Kellenberger
2019-21 Benjamin Kellenberger
'''

import os
import argparse
from psycopg2 import sql
from util.helpers import valid_image_extensions, listDirectory


if __name__ == '__main__':

parser = argparse.ArgumentParser(description='Parse YOLO annotations and import into database.')
parser = argparse.ArgumentParser(description='Import images into database.')
parser.add_argument('--project', type=str,
help='Shortname of the project to insert the images into.')
parser.add_argument('--settings_filepath', type=str, default='config/settings.ini', const=1, nargs='?',
help='Manual specification of the directory of the settings.ini file; only considered if environment variable unset (default: "config/settings.ini").')
args = parser.parse_args()
Expand All @@ -47,7 +50,7 @@
dbConn = Database(config)
if dbConn.connectionPool is None:
raise Exception('Error connecting to database.')
dbSchema = config.getProperty('Database', 'schema')
project = args.project


# check if running on file server
Expand Down Expand Up @@ -77,20 +80,20 @@

# ignore images that are already in database
print('Filter images already in database...')
imgs_existing = dbConn.execute('''
SELECT filename FROM {}.image;
'''.format(dbSchema), None, 'all')
imgs_existing = dbConn.execute(sql.SQL('''
SELECT filename FROM {};
''').format(sql.Identifier(project, 'image'), None, 'all')
imgs_existing = set([i['filename'] for i in imgs_existing])

imgs = list(imgs.difference(imgs_existing))
imgs = [(i,) for i in imgs]

# push image to database
print('Adding to database...')
dbConn.insert('''
INSERT INTO {}.image (filename)
dbConn.insert(sql.SQL('''
INSERT INTO {} (filename)
VALUES %s;
'''.format(dbSchema),
''').format(sql.Identifier(project, 'image'),
imgs)

print('Done.')
print('Done.')

0 comments on commit 18efa4c

Please sign in to comment.