""" API operations on the contents of a library. """ import logging, os, string, shutil, urllib, re, socket from cgi import escape, FieldStorage from galaxy import util, datatypes, jobs, web, util from galaxy.web.base.controller import * from galaxy.util.sanitize_html import sanitize_html from galaxy.model.orm import * from DB import * log = logging.getLogger( __name__ ) class UploadController( BaseController ): @web.expose_api def index( self, trans, user_id, **kwd): """ """ @web.expose_api def create( self, trans, user_id, payload, **kwd): """ POST /api/user/:userid/upload [ filename : filesize : tags : pValue : format: folder: type: filebody : ] Request to upload file. We do not sure that this request will work with big files. If any problems will arise then we can receive files by splitting to parts or download them by URL. Folder is optional parameter. If it's not set then new folder will be created. Example: {id : 'a56d56', folder: 'a56d54' name : 'intervals1', format: 'raw', tags : [], pValue: 0.83} """ payload['filesystem_paths'] = payload.pop('filename') payload['create_type'] = 'file' payload['file_type'] = 'auto' payload['upload_option'] = 'upload_paths' library_id = 'f2db41e1fa331b3e' # TODO check library GenomeTrax is exist then get it's id otherwise create this library rval = {} try: library = trans.sa_session.query( trans.app.model.Library ).get( trans.security.decode_id(library_id) ) except: library = None if not library: trans.response.status = 400 return "Invalid library id ( %s ) specified." % str( library_id ) if 'folder' not in payload: folderparams = {} folderparams['name'] = payload['filesystem_paths'].split('/')[-1] folderparams['create_type'] = 'folder' folderparams['description'] = '' status, output = trans.webapp.controllers['library_common'].create_folder( trans, 'api', trans.security.encode_id(library.root_folder.id), library_id, **folderparams ) for k, v in output.items(): if type( v ) == trans.app.model.LibraryDatasetDatasetAssociation: v = v.library_dataset folder_id = trans.security.encode_id( 'folder.' + str( v.id ) ) break db = DB() tree = db.query("select tree_no from tree where name='UDM'") if len(tree) == 0: udm_no = db.execute("insert into tree values(0, 'UDM', '%s', -1, 'folder', 'av!h', '', now())" % user_id) else: udm_no = tree[0][0] tree_no = db.execute("insert into tree values(0, '%s', '%s', %d, 'subset', '!v', '', now())" % (folderparams['name'], user_id, udm_no)) db.execute("insert into tree_to_galaxy values(0, '%s', %d, '%s')" % (user_id, tree_no, folder_id)) db.close() else: folder_id = str( payload.pop( 'folder_id' ) ) decoded_type_and_id = trans.security.decode_string_id( folder_id ) parent_type, decoded_folder_id = decoded_type_and_id.split( '.' ) status, output = trans.webapp.controllers['library_common'].upload_library_dataset( trans, 'api', library_id, trans.security.encode_id(decoded_folder_id), **payload ) if status == 200: for k, v in output.items(): if type( v ) == trans.app.model.LibraryDatasetDatasetAssociation: v = v.library_dataset encoded_id = trans.security.encode_id( 'file.' + str( v.id ) ) rval['id'] = encoded_id rval['folder'] = folder_id rval['name'] = v.name # rval['format'] = 'raw' # rval['tags'] = [] # rval['pValue'] = 0.83 return rval