diff --git a/extras/db.sql b/extras/db.sql index eda659b6..8071497c 100644 --- a/extras/db.sql +++ b/extras/db.sql @@ -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 -- ################################################## diff --git a/lega/keyserver.py b/lega/keyserver.py index e32839fa..0f6bf2e4 100644 --- a/lega/keyserver.py +++ b/lega/keyserver.py @@ -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): diff --git a/lega/utils/db.py b/lega/utils/db.py index 6aef8b3f..35ad58b2 100644 --- a/lega/utils/db.py +++ b/lega/utils/db.py @@ -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