Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

Commit

Permalink
Merge pull request #265 from NBISweden/feature/file-size
Browse files Browse the repository at this point in the history
Temp endpoint for file returns more stuff
  • Loading branch information
dtitov authored Mar 19, 2018
2 parents 72b9f5d + ede38e8 commit b6d755d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
11 changes: 0 additions & 11 deletions extras/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@ CREATE FUNCTION insert_file(filename files.filename%TYPE,
END;
$insert_file$ LANGUAGE plpgsql;

CREATE FUNCTION translate_fileid_to_filepath(sid files.stable_id%TYPE)
RETURNS files.filepath%TYPE AS $translate_fileid_to_filepath$
#variable_conflict use_column
DECLARE
filepath files.filepath%TYPE;
BEGIN
SELECT filepath FROM files WHERE stable_id = sid LIMIT 1 INTO filepath;
RETURN filepath;
END;
$translate_fileid_to_filepath$ LANGUAGE plpgsql;

-- ##################################################
-- ERRORS
-- ##################################################
Expand Down
19 changes: 12 additions & 7 deletions lega/keyserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,14 +459,19 @@ async def check_ttl(request):
return web.HTTPBadRequest()

@routes.get('/temp/file/{file_id}')
async def translate_file_id_to_filepath(request):
"""Translate a file_id to a file_path"""
async def id2info(request):
"""Translate a file_id to a file info"""
file_id = request.match_info['file_id']
LOG.debug(f'Translation {file_id} to filepath')
filepath = await db.get_filepath(request.app['db'], file_id)
LOG.debug(f'Filepath {filepath}')
if filepath:
return web.Response(text=filepath)
LOG.debug(f'Translation {file_id} to fileinfo')
fileinfo = await db.get_fileinfo(request.app['db'], file_id)
if fileinfo:
filepath, filesize, checksum, algo = fileinfo # unpack
return web.json_response({
'filepath': filepath,
'filesize': filesize,
'checksum': checksum,
'algo': algo,
})
raise web.HTTPNotFound(text=f'Dunno anything about a file with id "{file_id}"\n')

async def load_keys_conf(store):
Expand Down
8 changes: 4 additions & 4 deletions lega/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,14 @@ async def create_pool(loop):
db_args = fetch_args(CONF)
return await aiopg.create_pool(**db_args, loop=loop, echo=True)

async def get_filepath(conn, file_id):
async def get_fileinfo(conn, file_id):
assert file_id, 'Eh? No file ID?'
try:
LOG.debug(f'File Info for {file_id}')
with (await conn.cursor()) as cur:
query = 'SELECT translate_fileid_to_filepath(%(file_id)s)'
#query = "SELECT filepath from files where stable_id = '%(file_id)s';"
query = "SELECT filepath, reenc_size, reenc_checksum, 'sha256' FROM files WHERE stable_id = %(file_id)s LIMIT 1;"
await cur.execute(query, {'file_id': file_id})
return (await cur.fetchone())[0]
return (await cur.fetchone())
except psycopg2.InternalError as pgerr:
LOG.debug(f'File Info for {file_id}: {pgerr!r}')
return None
Expand Down

0 comments on commit b6d755d

Please sign in to comment.