예제 #1
0
파일: files.py 프로젝트: dativebase/old
def serve_file(id, reduced=False):
    """Serve the content (binary data) of a file.
    
    :param str id: the ``id`` value of the file whose file data will be served.
    :param bool reduced: toggles serving of file data or reduced-size file data.

    """
    file = Session.query(File).options(subqueryload(File.parent_file)).get(id)
    if getattr(file, 'parent_file', None):
        file = file.parent_file
    elif getattr(file, 'url', None):
        response.status_int = 400
        return json.dumps({'error': u'The content of file %s is stored elsewhere at %s' % (id, file.url)})
    if file:
        files_dir = h.get_OLD_directory_path('files', config=config)
        if reduced:
            filename = getattr(file, 'lossy_filename', None)
            if not filename:
                response.status_int = 404
                return json.dumps({'error': u'There is no size-reduced copy of file %s' % id})
            file_path = os.path.join(files_dir, 'reduced_files', filename)
        else:
            file_path = os.path.join(files_dir, file.filename)
        unrestricted_users = h.get_unrestricted_users()
        if h.user_is_authorized_to_access_model(session['user'], file, unrestricted_users):
            return forward(FileApp(file_path))
        else:
            response.status_int = 403
            return json.dumps(h.unauthorized_msg)
    else:
        response.status_int = 404
        return json.dumps({'error': 'There is no file with id %s' % id})
예제 #2
0
    def beta_qlt(self, id=None):
        c.beta = True

        setup, struct = self.__load_setup(id)
        if not setup:
            abort(404)
        
        quadrant = request.params.get('quadrant', None)
        if not quadrant or quadrant not in ('A','B','C','D','E','F','G','H'):
            abort(404)
        
        plate_layout = struct.get('plate_layout', None)
        if not plate_layout:
            abort(404)
        
        # TODO this line of code sucks (this whole thing sucks)
        if plate_layout[2] in ('s', 'sh', 'sw'):
            qlt_file = "%s/%s" % (config['qlb.setup_template_store'], plate_layout[0])
        else:
            qlt_file = "%s/%s" % (config['qlb.setup_template_store'], (plate_layout[0] % quadrant))
        
        # figure out which plate layout to load
        response.headers['Content-Type'] = 'application/quantalife-template'
        h.set_download_response_header(request, response, "%s.qlt" % ("%s_%s" % (setup.prefix, quadrant)))
        response.headers['Pragma'] = 'no-cache'
        response.headers['Cache-Control'] = 'no-cache'
        return forward(FileApp(qlt_file, response.headerlist))
예제 #3
0
    def preview(self, model_uniq, args):
        class_id = int(args.get('class', -1))
        sample_id = int(args.get('sample', -1))
        log.debug('Preview model %s, class_id %s, sample_id %s', model_uniq,
                  class_id, sample_id)

        # if class_id < 0:
        #     response.headers['Content-Type'] = 'text/xml'
        #     return '<model />'

        # if sample_id < 0:
        #     response.headers['Content-Type'] = 'text/xml'
        #     return '<class />'

        # if asking for a sample from a specific class
        if class_id >= 0 and sample_id >= 0:
            # load thumbnail for requested class and sample_id
            model = self.get_model(model_uniq)
            path = os.path.join(model.path, 'class_{0:05d}'.format(class_id))
            thumbnail = os.path.join(path,
                                     'sample_{0:05d}.png'.format(sample_id))
            if not os.path.exists(thumbnail):
                _mkdir(path)
                model.cache_sample_preview(class_id, sample_id, thumbnail)
                # if asking for a model's thumbnail
        elif class_id < 0 or sample_id < 0:
            model = self.get_model(model_uniq)
            thumbnail = '%s/thumbnail.svg' % model.path

        return forward(
            FileApp(
                thumbnail,
                content_type='image/png',
                #content_disposition=disposition,
            ).cache_control(max_age=60 * 60 * 24 * 7 * 6))  # 6 weeks
예제 #4
0
    def return_from_workdir(self, table, resource_list):
        """
            Returns a hdf5 file from the workdir

            @param: table - workdir table object that allows access
            to the workdir table created by the feature service
            @param: resource_list - the resource lists object containing
            all the resources proccessed on during the request

            @yield: fileapp object with path set to the hdf5 file in
            the feature workdir
        """
        # since the uncached table is already saved in the workdir the file is just
        # returned
        try:
            disposition = 'attachment; filename="%s"' % (
                table.filename).encode('ascii')
        except UnicodeEncodeError:
            disposition = 'attachment; filename="%s"; filename*="%s"' % (
                (table.filename).encode('utf8'),
                (table.filename).encode('utf8'))

        #waits table that is being constructed
        with Locks(table.path):
            pass

        return forward(
            FileApp(table.path,
                    allowed_methods=('GET', 'POST'),
                    content_type=self.content_type,
                    content_disposition=disposition))
예제 #5
0
파일: corpora.py 프로젝트: jrwdunham/old
    def servefile(self, id, file_id):
        """Return the corpus as a file in the format specified in the URL query string.

        :URL: ``PUT /corpora/id/servefile/file_id``.
        :param str id: the ``id`` value of the corpus.
        :param str file_id: the ``id`` value of the corpus file.
        :returns: the file data

        """
        corpus = Session.query(Corpus).get(id)
        if corpus:
            try:
                corpus_file = filter(lambda cf: cf.id == int(file_id), corpus.files)[0]
                corpus_file_path = os.path.join(get_corpus_dir_path(corpus),
                                              '%s.gz' % corpus_file.filename)
                if authorized_to_access_corpus_file(session['user'], corpus_file):
                    return forward(FileApp(corpus_file_path, content_type='application/x-gzip'))
                else:
                    response.status_int = 403
                    return json.dumps(h.unauthorized_msg)
            except Exception:
                response.status_int = 400
                return json.dumps({'error': 'Unable to serve corpus file %d of corpus %d' % (
                        file_id, id)})
        else:
            response.status_int = 404
            return json.dumps({'error': 'There is no corpus with id %s' % id})
예제 #6
0
    def batch_plate_template_download(self, id=None):
        plate = self.__load_batch_plate(id)
        box2 = Session.query(Box2).get(self.form_result['box2_id'])
        if not plate or not box2:
            abort(404)
            
        code = plate.batch.plate_type.code

        if self.form_result['qc_plate']:
            plate.qc_plate = self.form_result['qc_plate']
            Session.commit()

        # TODO FIXFIX or incorporate into model
        if plate.qc_plate:
            serial = 'QC'
        else:
            serial = box2.name.split(' ')[-1]
        # only mfgco supported right now
        if code == 'mfgco':
            qlt_file = "%s/carryover.qlt" % config['qlb.setup_template_store']
        elif code == 'fvtitr':
            qlt_file = "%s/fvbatch_QC.qlt" % config['qlb.setup_template_store']
        else:
            abort(404)
        
        response.headers['Content-Type'] = 'application/quantalife-template'
        h.set_download_response_header(request, response, "%s_%s.qlt" % (serial, plate.name))
        response.headers['Pragma'] = 'no-cache'
        response.headers['Cache-Control'] = 'no-cache'
        return forward(FileApp(qlt_file, response.headerlist))
예제 #7
0
    def servefile(self, id, file_id):
        """Return the corpus as a file in the format specified in the URL query string.

        :URL: ``PUT /corpora/id/servefile/file_id``.
        :param str id: the ``id`` value of the corpus.
        :param str file_id: the ``id`` value of the corpus file.
        :returns: the file data

        """
        corpus = Session.query(Corpus).get(id)
        if corpus:
            try:
                corpus_file = filter(lambda cf: cf.id == int(file_id),
                                     corpus.files)[0]
                corpus_file_path = os.path.join(get_corpus_dir_path(corpus),
                                                '%s.gz' % corpus_file.filename)
                if authorized_to_access_corpus_file(session['user'],
                                                    corpus_file):
                    return forward(
                        FileApp(corpus_file_path,
                                content_type='application/x-gzip'))
                else:
                    response.status_int = 403
                    return json.dumps(h.unauthorized_msg)
            except Exception:
                response.status_int = 400
                return json.dumps({
                    'error':
                    'Unable to serve corpus file %d of corpus %d' %
                    (file_id, id)
                })
        else:
            response.status_int = 404
            return json.dumps({'error': 'There is no corpus with id %s' % id})
예제 #8
0
def serve_file(file, attachment=False):
    headers = [('Content-Disposition', 'attachment; filename="%s"' % file.filename.encode('utf-8'))]
    content_type, content_encoding = mimetypes.guess_type(file.filename.encode('utf-8'))
    kwargs = {'content_type': content_type}
    if content_type in ['image/png', 'image/jpeg'] and not attachment:
        headers = [('Content-Disposition', 'inline; filename="%s"' % file.filename.encode('utf-8'))]
    return forward(FileApp(file.filepath(), headers=headers, **kwargs))
예제 #9
0
 def export_deprecated(self, id):
     """Export the parser as a self-contained archive including a Python interface and all required files.
     """
     try:
         parser = Session.query(MorphologicalParser).get(id)
         directory = parser.directory
         archive_dir = os.path.join(directory, 'archive')
         if os.path.exists(archive_dir):
             rmtree(archive_dir)
         os.mkdir(archive_dir)
         parser.copy_files(archive_dir)
         parser.phonology.copy_files(archive_dir)
         parser.morphology.copy_files(archive_dir)
         parser.language_model.copy_files(archive_dir)
         lib_path = os.path.join(config['here'], 'onlinelinguisticdatabase', 'lib')
         simplelm_path = os.path.join(lib_path, 'simplelm')
         parser_path = os.path.join(lib_path, 'parser.py')
         parse_path = os.path.join(lib_path, 'parse.py')
         new_parse_path = os.path.join(archive_dir, 'parse.py')
         copytree(simplelm_path, os.path.join(archive_dir, 'simplelm'))
         copyfile(parser_path, os.path.join(archive_dir, 'parser.py'))
         copyfile(parse_path, new_parse_path)
         os.chmod(new_parse_path, 0744)
         data = parser.export()
         data_path = os.path.join(archive_dir, 'data.pickle')
         cPickle.dump(data, open(data_path, 'wb'))
         zip_path = h.zipdir(archive_dir)
         return forward(FileApp(zip_path))
     except Exception, e:
         log.warn(e)
         response.status_int = 400
         return json.dumps({'error': 'An error occured while attempting to export '
                            'morphological parser %s' % id})
예제 #10
0
    def serve_arpa(self, id):
        """Serve the generated ARPA file of the morpheme language model.

        :URL: ``PUT /morphemelanguagemodels/serve_arpa/id``
        :param str id: the ``id`` value of a morpheme language model.
        :returns: a stream of bytes -- the ARPA file of the LM.

        """
        lm = Session.query(MorphemeLanguageModel).get(id)
        if lm:
            arpa_path = lm.get_file_path('arpa')
            if os.path.isfile(arpa_path):
                if authorized_to_access_arpa_file(session['user'], lm):
                    return forward(
                        FileApp(arpa_path, content_type='text/plain'))
                else:
                    response.status_int = 403
                    return json.dumps(h.unauthorized_msg)
            else:
                response.status_int = 404
                return json.dumps({
                    'error':
                    'The ARPA file for morpheme language model %s has not been compiled yet.'
                    % id
                })
        else:
            response.status_int = 404
            return json.dumps({
                'error':
                'There is no morpheme language model with id %s' % id
            })
예제 #11
0
    def serve_entry_point(self,
                          node,
                          default='Entry point was not found...',
                          **kw):
        """Provide a general way to serve an entry point"""

        log.debug('Serving entry point: %s', node)

        if isinstance(node, str) is True:
            node = self.module_xml.xpath(node)

        text = default
        if len(node) > 0:  # Found at least one xpath match
            node = node[0]
            log.debug('Node: %s', etree.tostring(node))

            type = node.get('type', None)
            if type == 'file':
                path = self.filepath(node.get('value'))
                log.debug('Serving file: %s', path)
                if os.path.exists(path):
                    return forward(
                        FileApp(path).cache_control(max_age=60 * 60 * 24 * 7 *
                                                    6))

            else:
                text = node.get('value', None)
                if node.get('value', None) is None:
                    # Using a <value><![CDATA]</value>
                    text = (node[0].text)

        if text is None:
            abort(404)
        return text
예제 #12
0
    def servecompiled(self, id):
        """Serve the compiled foma script of the morphophonology FST of the morphological parser.

        :URL: ``PUT /morphologicalparsers/servecompiled/id``
        :param str id: the ``id`` value of a morphological parser.
        :returns: a stream of bytes -- the compiled morphological parser script.  

        """
        parser = Session.query(MorphologicalParser).get(id)
        if parser:
            if h.foma_installed():
                binary_path = parser.get_file_path('binary')
                if os.path.isfile(binary_path):
                    return forward(FileApp(binary_path))
                else:
                    response.status_int = 400
                    return json.dumps({
                        'error':
                        'The morphophonology foma script of '
                        'MorphologicalParser %d has not been compiled yet.' %
                        parser.id
                    })
            else:
                response.status_int = 400
                return json.dumps(
                    {'error': 'Foma and flookup are not installed.'})
        else:
            response.status_int = 404
            return json.dumps(
                {'error': 'There is no morphological parser with id %s' % id})
예제 #13
0
    def serve(self, id, download=False, **kwargs):
        """Serve a :class:`~mediacore.model.media.MediaFile` binary.

        :param id: File ID
        :type id: ``int``
        :param bool download: If true, serve with an Content-Disposition that
            makes the file download to the users computer instead of playing
            in the browser.
        :raises webob.exc.HTTPNotFound: If no file exists with this ID.
        :raises webob.exc.HTTPNotAcceptable: If an Accept header field
            is present, and if the mimetype of the requested file doesn't
            match, then a 406 (not acceptable) response is returned.

        """
        file = fetch_row(MediaFile, id=id)

        file_path = helpers.file_path(file).encode('utf-8')
        file_type = file.mimetype.encode('utf-8')
        file_name = file.display_name.encode('utf-8')

        if not os.path.exists(file_path):
            log.warn('No such file or directory: %r', file_path)
            raise HTTPNotFound()

        # Ensure the request accepts files with this container
        accept = request.environ.get('HTTP_ACCEPT', '*/*')
        if not mimeparse.best_match([file_type], accept):
            raise HTTPNotAcceptable() # 406

        method = config.get('file_serve_method', None)
        headers = []

        # Serving files with this header breaks playback on iPhone
        if download:
            headers.append(('Content-Disposition',
                            'attachment; filename="%s"' % file_name))

        if method == 'apache_xsendfile':
            # Requires mod_xsendfile for Apache 2.x
            # XXX: Don't send Content-Length or Etag headers,
            #      Apache handles them for you.
            response.headers['X-Sendfile'] = file_path
            response.body = ''
            response.headers.update(headers)
        elif method == 'nginx_redirect':
            for header in ['pragma', 'cache-control', "content-type"]:
                if header in response.headers:
                    del response.headers[header]
            response.headers['X-Accel-Redirect'] = file_path
            response.headers.update(headers)
        else:
            app = FileApp(file_path, headers, content_type=file_type)
            return forward(app)

        response.headers['Content-Type'] = file_type
        for header, value in headers:
            response.headers[header] = value

        return None
예제 #14
0
파일: root.py 프로젝트: smarkm/ovm
 def xmlrpc(self, *args, **kw):
     try:
         self.authenticate()
     
     except Exception as e:
         raise e
     
     return forward(RootXMLRPCController())
예제 #15
0
    def retrieve(self, path):
        """retrieve action is referenced by the <a>, <img>, <audio>, <video>,
        <embed>, etc. tags.

        """

        path = os.path.join(config['app_conf']['permanent_store'], path)
        app = FileApp(path)
        return forward(app)
예제 #16
0
    def retrieve(self, path):
        """retrieve action is referenced by the <a>, <img>, <audio>, <video>,
        <embed>, etc. tags.

        """

        path = os.path.join(config['app_conf']['permanent_store'], path)
        app = FileApp(path)
        return forward(app)
예제 #17
0
    def retrieve_temp(self, path):
        """retrieve_temp action is referenced by the <a> button rendered in
        /derived/file/export.html.

        """

        path = os.path.join(config['app_conf']['temporary_store'], path)
        app = FileApp(path)
        return forward(app) 
예제 #18
0
    def retrieve_temp(self, path):
        """retrieve_temp action is referenced by the <a> button rendered in
        /derived/file/export.html.

        """

        path = os.path.join(config['app_conf']['temporary_store'], path)
        app = FileApp(path)
        return forward(app)
예제 #19
0
    def as_text(cls, slug, username, key):
        filepath = join(cls.datapath, slug, username, '%s.txt' % key)
        headers = [('Content-Type', 'text/plain'), ('Content-Disposition', 'attachment; filename=%s' % key) ]

        try:
            text = forward(FileApp(filepath, headers))
        except OSError:
            abort(404, 'Game does not exist: %s' % slug)
        else:
            return text
예제 #20
0
    def as_text(cls, slug, username, key):
        filepath = join(cls.datapath, slug, username, '%s.txt' % key)
        headers = [('Content-Type', 'text/plain'),
                   ('Content-Disposition', 'attachment; filename=%s' % key)]

        try:
            text = forward(FileApp(filepath, headers))
        except OSError:
            abort(404, 'Game does not exist: %s' % slug)
        else:
            return text
예제 #21
0
    def public(self, *path, **kw):
        """Deliver static content for the Module"""
        log.debug("in Static path=%s kw=%s", path, kw)

        static_path = os.path.join(self.path, 'public', *path)
        if os.path.exists(static_path):
            return forward(
                FileApp(static_path).cache_control(max_age=60 * 60 * 24 * 7 *
                                                   6))

        raise abort(404)
예제 #22
0
    def download_file(self, id=None):
        self.__setup_box2_code_context(id)
        thefile = self.__file_id_query(c.box2.id, self.form_result['file_id'])
        if not thefile:
            abort(404)

        h.set_download_response_header(request, response, thefile.name)
        response.content_type = thefile.mime_type
        file_path = self.__upload_file_path(c.box2, thefile.path)
        response.headers['Pragma'] = 'no-cache'
        response.headers['Cache-Control'] = 'no-cache'
        return forward(FileApp(file_path, response.headerlist))
예제 #23
0
 def get(self, id):
     """
     To get the PDF created previously.
     """
     name = gettempdir() + sep + self.TEMP_FILE_PREFIX + id + self.TEMP_FILE_SUFFIX
     headers = {
         'Content-Length' : getsize(name),
         'Content-Type' : 'application/pdf',
         'Content-Disposition' : 'attachment; filename='+id+'.pdf',
         'Pragma' : 'public',
         'Expires' : '0',
         'Cache-Control' : 'private'
         }
     return forward(FileApp(name, **headers))
예제 #24
0
    def export(self, id):
        """Export the parser as a self-contained .zip archive including a Python interface and all required files.

        This allows a user to use the parser locally (assuming they have foma and MITLM installed) via
        the following procedure:

            $ unzip archive.zip
            $ cd archive
            $ ./parse.py chiens chats tombait

        """
        try:
            parser = Session.query(MorphologicalParser).get(id)
            directory = parser.directory
            lib_path = os.path.abspath(os.path.dirname(h.__file__))

            # config.pickle is a dict used to construct the parser (see lib/parse.py)
            config_ = parser.export()
            config_path = os.path.join(directory, 'config.pickle')
            cPickle.dump(config_, open(config_path, 'wb'))

            # cache.pickle is a dict encoding the cached parses of this parser
            cache_dict = parser.cache.export()
            cache_path = os.path.join(directory, 'cache.pickle')
            cPickle.dump(cache_dict, open(cache_path, 'wb'))

            # create the .zip archive, including the files of the parser, the simplelm package,
            # the parser.py module and the parse.py executable.
            zip_path = os.path.join(directory, 'archive.zip')
            zip_file = h.ZipFile(zip_path, 'w')
            #zip_file.write_directory(parser.directory)
            for file_name in os.listdir(directory):
                if (os.path.splitext(file_name)[1]
                        not in ('.log', '.sh', '.zip')
                        and file_name != 'morpheme_language_model.pickle'):
                    zip_file.write_file(os.path.join(directory, file_name))
            zip_file.write_directory(os.path.join(lib_path, 'simplelm'),
                                     keep_dir=True)
            zip_file.write_file(os.path.join(lib_path, 'parser.py'))
            zip_file.write_file(os.path.join(lib_path, 'parse.py'))
            zip_file.close()
            return forward(FileApp(zip_path))
        except Exception, e:
            log.warn(e)
            response.status_int = 400
            return json.dumps({
                'error':
                'An error occured while attempting to export '
                'morphological parser %s: %s' % (id, e)
            })
예제 #25
0
    def validation_download(self):
        plate_template = Session.query(PlateTemplate).get(self.form_result['plate_template_id'])
        plate_type = Session.query(PlateType).filter_by(code=self.form_result['plate_type_code']).first()
        plate_template.plate_type_id = plate_type.id
        Session.commit()

        response.headers['Content-Type'] = 'application/quantalife-template'
        h.set_download_response_header(request, response, "%s.qlt" % plate_template.prefix)
        response.headers['Pragma'] = 'no-cache'

        # yeah, this is somewhat dangerous...
        template_path = "%s/%s" % (config['qlb.setup_template_store'], os.path.basename(self.form_result['template_name']))

        return forward(FileApp(template_path, response.headerlist))
예제 #26
0
 def get(self, id):
     """
     To get the PDF created previously.
     """
     name = gettempdir(
     ) + sep + self.TEMP_FILE_PREFIX + id + self.TEMP_FILE_SUFFIX
     headers = {
         'Content-Length': getsize(name),
         'Content-Type': 'application/pdf',
         'Content-Disposition': 'attachment; filename=' + id + '.pdf',
         'Pragma': 'public',
         'Expires': '0',
         'Cache-Control': 'private'
     }
     return forward(FileApp(name, **headers))
예제 #27
0
 def download(self, id=None, *args, **kwargs):
     if id is None:
         abort(404)
     
     qlbwell = Session.query(QLBWell).get(id)
     if not qlbwell:
         abort(404)
     
     if not qlbwell.file:
         abort(404)
     
     storage = QLStorageSource(config)
     response.headers['Content-Type'] = 'application/quantalife-raw'
     h.set_download_response_header(request, response, qlbwell.file.basename)
     return forward(FileApp(storage.qlbwell_path(qlbwell), response.headerlist))
예제 #28
0
    def export(self, id):
        """Export the parser as a self-contained .zip archive including a Python interface and all required files.

        This allows a user to use the parser locally (assuming they have foma and MITLM installed) via
        the following procedure:

            $ unzip archive.zip
            $ cd archive
            $ ./parse.py chiens chats tombait

        """
        try:
            parser = Session.query(MorphologicalParser).get(id)
            directory = parser.directory
            lib_path = os.path.abspath(os.path.dirname(h.__file__))

            # config.pickle is a dict used to construct the parser (see lib/parse.py)
            config_ = parser.export()
            config_path = os.path.join(directory, 'config.pickle')
            cPickle.dump(config_, open(config_path, 'wb'))

            # cache.pickle is a dict encoding the cached parses of this parser
            cache_dict = parser.cache.export()
            cache_path = os.path.join(directory, 'cache.pickle')
            cPickle.dump(cache_dict, open(cache_path, 'wb'))

            # create the .zip archive, including the files of the parser, the simplelm package,
            # the parser.py module and the parse.py executable.
            zip_path = os.path.join(directory, 'archive.zip')
            zip_file = h.ZipFile(zip_path, 'w')
            #zip_file.write_directory(parser.directory)
            for file_name in os.listdir(directory):
                if (os.path.splitext(file_name)[1] not in ('.log', '.sh', '.zip') and
                    file_name != 'morpheme_language_model.pickle'):
                    zip_file.write_file(os.path.join(directory, file_name))
            zip_file.write_directory(os.path.join(lib_path, 'simplelm'), keep_dir=True)
            zip_file.write_file(os.path.join(lib_path, 'parser.py'))
            zip_file.write_file(os.path.join(lib_path, 'parse.py'))
            zip_file.close()
            return forward(FileApp(zip_path))
        except Exception, e:
            log.warn(e)
            response.status_int = 400
            return json.dumps({'error': 'An error occured while attempting to export '
                'morphological parser %s: %s' % (id, e)})
예제 #29
0
    def get_one(self, ident, **kw):
        "Fetch a blob based on uniq ID"
        log.info("get_one(%s) %s", ident, kw)
        try:
            if not is_uniq_code(ident):
                abort(404, "Must be resource unique code")

            resource = data_service.resource_load(uniq=ident)
            if resource is None:
                abort(403, "resource does not exist or permission denied")
            filename, _ = split_subpath(resource.get('name', str(ident)))
            blb = self.localpath(ident)
            if blb.files and len(blb.files) > 1:
                return export_service.export(files=[resource.get('uri')],
                                             filename=filename)

            localpath = os.path.normpath(blb.path)
            if 'localpath' in kw:
                tg.response.headers['Content-Type'] = 'text/xml'
                resource = etree.Element('resource',
                                         name=filename,
                                         value=localpath)
                return etree.tostring(resource)

            disposition = '' if 'noattach' in kw else 'attachment; '
            try:
                disposition = '%sfilename="%s"' % (disposition,
                                                   filename.encode('ascii'))
            except UnicodeEncodeError:
                disposition = '%sfilename="%s"; filename*="%s"' % (
                    disposition, filename.encode('utf8'),
                    filename.encode('utf8'))

            content_type = self.guess_mime(filename)
            return forward(
                BQFileApp(
                    localpath,
                    content_type=content_type,
                    content_disposition=disposition,
                ).cache_control(max_age=60 * 60 * 24 * 7 * 6))  # 6 weeks
        except IllegalOperation:
            abort(404, "Error occurent fetching blob")
예제 #30
0
    def crossdomain_xml(self, **kwargs):
        """Serve the crossdomain XML file manually if static_files is disabled.

        If someone forgets to add this Alias we might as well serve this file
        for them and save everyone the trouble. This only works when MediaCore
        is served out of the root of a domain and if Cooliris is enabled.
        """
        global crossdomain_app

        if not request.settings['appearance_enable_cooliris']:
            # Ensure the cache is cleared if cooliris is suddenly disabled
            if crossdomain_app:
                crossdomain_app = None
            raise HTTPNotFound()

        if not crossdomain_app:
            relpath = 'mediacore/public/crossdomain.xml'
            abspath = os.path.join(config['here'], relpath)
            crossdomain_app = FileApp(abspath)

        return forward(crossdomain_app)
예제 #31
0
    def crossdomain_xml(self, **kwargs):
        """Serve the crossdomain XML file manually if static_files is disabled.

        If someone forgets to add this Alias we might as well serve this file
        for them and save everyone the trouble. This only works when MediaCore
        is served out of the root of a domain and if Cooliris is enabled.
        """
        global crossdomain_app

        if not request.settings['appearance_enable_cooliris']:
            # Ensure the cache is cleared if cooliris is suddenly disabled
            if crossdomain_app:
                crossdomain_app = None
            raise HTTPNotFound().exception

        if not crossdomain_app:
            relpath = 'mediacore/public/crossdomain.xml'
            abspath = os.path.join(config['here'], relpath)
            crossdomain_app = FileApp(abspath)

        return forward(crossdomain_app)
예제 #32
0
파일: root.py 프로젝트: agrynchuk/noodle-ng
 def proxyDownload(self, id=None, tar=False):
     
     if proxyDl==True:
         
         # get path to file with ID id from database
         item = DBSession.query(s.share).filter(s.share.id == id).one()
         host = item.getHost()
         service = item.getService()
         filename = str( item.getShowPath().split("/")[-1] )
         
         if service.username and service.username != "anonymous":
             # uri = "smb://*****:*****@hostip/path/to/file"
             uri = u"smb://%s:%s@%s%s" % ( service.username, service.password, host.ip, item.getShowPath() )
         else:
             # uri = "smb://hostip/path/to/file"
             uri = u"smb://%s%s" % ( host.ip, item.getShowPath() )
         
         #imports moved to the top
         f = smbfileapp.FileApp(uri)
         return forward(f)
     else: #proxyDl not enabled
         return "<p><strong>proxyDownloader is (currently) not available on this system!</strong><br /><em>Sorry.</em></p>\n"
예제 #33
0
    def servecompiled(self, id):
        """Serve the compiled foma script of the phonology.

        :URL: ``PUT /phonologies/servecompiled/id``
        :param str id: the ``id`` value of a phonology.
        :returns: a stream of bytes -- the compiled phonology script.  

        """
        phonology = Session.query(Phonology).get(id)
        if phonology:
            if h.foma_installed():
                compiled_path = phonology.get_file_path('binary')
                if os.path.isfile(compiled_path):
                    return forward(FileApp(compiled_path))
                else:
                    response.status_int = 400
                    return json.dumps({'error': 'Phonology %d has not been compiled yet.' % phonology.id})
            else:
                response.status_int = 400
                return json.dumps({'error': 'Foma and flookup are not installed.'})
        else:
            response.status_int = 404
            return json.dumps({'error': 'There is no phonology with id %s' % id})
예제 #34
0
 def export_deprecated(self, id):
     """Export the parser as a self-contained archive including a Python interface and all required files.
     """
     try:
         parser = Session.query(MorphologicalParser).get(id)
         directory = parser.directory
         archive_dir = os.path.join(directory, 'archive')
         if os.path.exists(archive_dir):
             rmtree(archive_dir)
         os.mkdir(archive_dir)
         parser.copy_files(archive_dir)
         parser.phonology.copy_files(archive_dir)
         parser.morphology.copy_files(archive_dir)
         parser.language_model.copy_files(archive_dir)
         lib_path = os.path.join(config['here'], 'onlinelinguisticdatabase',
                                 'lib')
         simplelm_path = os.path.join(lib_path, 'simplelm')
         parser_path = os.path.join(lib_path, 'parser.py')
         parse_path = os.path.join(lib_path, 'parse.py')
         new_parse_path = os.path.join(archive_dir, 'parse.py')
         copytree(simplelm_path, os.path.join(archive_dir, 'simplelm'))
         copyfile(parser_path, os.path.join(archive_dir, 'parser.py'))
         copyfile(parse_path, new_parse_path)
         os.chmod(new_parse_path, 0744)
         data = parser.export()
         data_path = os.path.join(archive_dir, 'data.pickle')
         cPickle.dump(data, open(data_path, 'wb'))
         zip_path = h.zipdir(archive_dir)
         return forward(FileApp(zip_path))
     except Exception, e:
         log.warn(e)
         response.status_int = 400
         return json.dumps({
             'error':
             'An error occured while attempting to export '
             'morphological parser %s' % id
         })
예제 #35
0
    def serve_arpa(self, id):
        """Serve the generated ARPA file of the morpheme language model.

        :URL: ``PUT /morphologies/serve_arpa/id``
        :param str id: the ``id`` value of a morpheme language model.
        :returns: a stream of bytes -- the ARPA file of the LM.

        """
        lm = Session.query(MorphemeLanguageModel).get(id)
        if lm:
            arpa_path = lm.get_file_path('arpa')
            if os.path.isfile(arpa_path):
                if authorized_to_access_arpa_file(session['user'], lm):
                    return forward(FileApp(arpa_path, content_type='text/plain'))
                else:
                    response.status_int = 403
                    return json.dumps(h.unauthorized_msg)
            else:
                response.status_int = 404
                return json.dumps({'error':
                    'The ARPA file for morpheme language model %s has not been compiled yet.' % id})
        else:
            response.status_int = 404
            return json.dumps({'error': 'There is no morpheme language model with id %s' % id})
예제 #36
0
    def servecompiled(self, id):
        """Serve the compiled foma script of the morphophonology FST of the morphological parser.

        :URL: ``PUT /morphologicalparsers/servecompiled/id``
        :param str id: the ``id`` value of a morphological parser.
        :returns: a stream of bytes -- the compiled morphological parser script.  

        """
        parser = Session.query(MorphologicalParser).get(id)
        if parser:
            if h.foma_installed():
                binary_path = parser.get_file_path('binary')
                if os.path.isfile(binary_path):
                    return forward(FileApp(binary_path))
                else:
                    response.status_int = 400
                    return json.dumps({'error': 'The morphophonology foma script of '
                        'MorphologicalParser %d has not been compiled yet.' % parser.id})
            else:
                response.status_int = 400
                return json.dumps({'error': 'Foma and flookup are not installed.'})
        else:
            response.status_int = 404
            return json.dumps({'error': 'There is no morphological parser with id %s' % id})
예제 #37
0
    def serve(self, id, download=False, **kwargs):
        """Serve a :class:`~mediadrop.model.media.MediaFile` binary.

        :param id: File ID
        :type id: ``int``
        :param bool download: If true, serve with an Content-Disposition that
            makes the file download to the users computer instead of playing
            in the browser.
        :raises webob.exc.HTTPNotFound: If no file exists with this ID.
        :raises webob.exc.HTTPNotAcceptable: If an Accept header field
            is present, and if the mimetype of the requested file doesn't
            match, then a 406 (not acceptable) response is returned.

        """
        file = fetch_row(MediaFile, id=id)
        request.perm.assert_permission(u'view', file.media.resource)

        file_type = file.mimetype.encode('utf-8')
        file_name = file.display_name.encode('utf-8')

        file_path = helpers.file_path(file)
        if file_path is None:
            log.warn('No path exists for requested media file: %r', file)
            raise HTTPNotFound()
        file_path = file_path.encode('utf-8')

        if not os.path.exists(file_path):
            log.warn('No such file or directory: %r', file_path)
            raise HTTPNotFound()

        # Ensure the request accepts files with this container
        accept = request.environ.get('HTTP_ACCEPT', '*/*')
        if not mimeparse.best_match([file_type], accept):
            raise HTTPNotAcceptable() # 406

        method = config.get('file_serve_method', None)
        headers = []

        # Serving files with this header breaks playback on iPhone
        if download:
            headers.append(('Content-Disposition',
                            'attachment; filename="%s"' % file_name))

        if method == 'apache_xsendfile':
            # Requires mod_xsendfile for Apache 2.x
            # XXX: Don't send Content-Length or Etag headers,
            #      Apache handles them for you.
            response.headers['X-Sendfile'] = file_path
            response.body = ''

        elif method == 'nginx_redirect':
            # Requires NGINX server configuration:
            # NGINX must have a location block configured that matches
            # the /__mediadrop_serve__ path below (the value configured by
            # setting  "nginx_serve_path" option in the configuration). It
            # should also be configured as an "internal" location to prevent
            # people from surfing directly to it.
            # For more information see: http://wiki.nginx.org/XSendfile
            serve_path = config.get('nginx_serve_path', '__mediadrop_serve__')
            if not serve_path.startswith('/'):
                serve_path = '/' + serve_path
            redirect_filename = '%s/%s' % (serve_path, os.path.basename(file_path))
            response.headers['X-Accel-Redirect'] = redirect_filename


        else:
            app = FileApp(file_path, headers, content_type=file_type)
            return forward(app)

        response.headers['Content-Type'] = file_type
        for header, value in headers:
            response.headers[header] = value

        return None
예제 #38
0
 def xmlrpc(self):
     return forward(HAXMLRPCController())
예제 #39
0
    def classify(self, model_uniq, args):
        image_uniq = args.get('classify')
        log.debug('Classify: %s on model %s', image_uniq, model_uniq)

        # ensure model is loaded
        model = self.get_model(model_uniq)
        #log.debug('Model: %s', str(model))

        args['_workdir'] = self.path_images
        args['_filename'] = '.'.join([
            '%s_%s' % (k, v) for k, v in args.iteritems()
            if not k.startswith('_')
        ])

        # get image
        image = XImage(image_uniq)
        log.debug('Image info: %s', image.info())

        # classify
        method = args.get('method', 'points').lower()
        if method not in self.classifiers.plugins:
            raise ConnoisseurException(
                responses.BAD_REQUEST,
                'Requested method "%s" not supported' % method)
        log.debug('Classifying using: %s', method)

        # format body if needed and augment args
        if '_http_body' in args:
            fmt_mime = args.get('_http_content_type',
                                [None])[0] or format_as_mime_type.get('xml')
            import_mime = '%s;%s' % (fmt_mime, method)
            if import_mime in self.importers_by_mime:
                token = self.importers_by_mime[import_mime].format(
                    data=args['_http_body'], args=args)

        # run classifier
        c = self.classifiers.plugins[method]
        token = c.classify(image, model, args)

        # format output
        fmt_mime = format_as_mime_type.get(args.get('format')) or args.get(
            '_http_accept', [None])[0] or format_as_mime_type.get('xml')
        export_mime = '%s;%s' % (token.mime, fmt_mime)
        log.debug('Export mime: %s', export_mime)
        if export_mime in self.exporters_by_mime:
            token = self.exporters_by_mime[export_mime].format(token,
                                                               args=args)

        # return output
        disposition = None
        if token.filename is not None:
            try:
                disposition = 'filename="%s"' % (
                    token.filename.encode('ascii'))
            except UnicodeEncodeError:
                disposition = 'filename="%s"; filename*="%s"' % (
                    token.filename.encode('utf8'),
                    token.filename.encode('utf8'))

        if token.isFile() is True:
            return forward(
                FileApp(
                    token.data,
                    content_type=token.mime,
                    content_disposition=disposition,
                ).cache_control(max_age=60 * 60 * 24 * 7 * 6))  # 6 weeks
        else:
            response.headers['Content-Type'] = token.mime
            response.headers['Content-Disposition'] = disposition
            return token.data
예제 #40
0
 def xmlrpc(self):
     return forward(ApplianceXMLRPCController())
예제 #41
0
            try:
                fname.encode('ascii')
                disposition = '%sfilename="%s"' % (disposition, fname)
            except UnicodeEncodeError:
                # dima: encode the filename in utf-8
                disposition = '%sfilename="%s"; filename*="%s"' % (
                    disposition, fname.encode('utf8'), fname.encode('utf8'))
                #disposition = '%sfilename="%s"; filename*="%s"'%(disposition, fname.encode('ascii', errors='ignore'), fname.encode('utf8'))

            # fix for the cherrypy error 10055 "No buffer space available" on windows
            # by streaming the contents of the files as opposite to sendall the whole thing
            log.info("FINISHED (%s): %s", datetime.now().isoformat(), url)
            #log.info ("%s: returning %s with mime %s"%(ident, token.data, token.contentType ))
            return forward(
                FileApp(
                    token.data,
                    content_type=token.contentType,
                    content_disposition=disposition,
                ).cache_control(max_age=60 * 60 * 24 * 7 * 6))  # 6 weeks

        log.info("FINISHED with ERROR (%s): %s",
                 datetime.now().isoformat(), url)
        tg.response.status_int = 404
        return "File not found"


def initialize(uri):
    """ Initialize the top level server for this microapp"""
    # Add you checks and database initialize
    log.debug("initialize " + uri)
    service = ImageServiceController(uri)
    #directory.register_service ('image_service', service)
예제 #42
0
 def xmlrpc(self):
     return forward(StorageXMLRPCController())
예제 #43
0
    def serve(self, id, download=False, **kwargs):
        """Serve a :class:`~mediacore.model.media.MediaFile` binary.

        :param id: File ID
        :type id: ``int``
        :param bool download: If true, serve with an Content-Disposition that
            makes the file download to the users computer instead of playing
            in the browser.
        :raises webob.exc.HTTPNotFound: If no file exists with this ID.
        :raises webob.exc.HTTPNotAcceptable: If an Accept header field
            is present, and if the mimetype of the requested file doesn't
            match, then a 406 (not acceptable) response is returned.

        """
        file = fetch_row(MediaFile, id=id)

        file_type = file.mimetype.encode('utf-8')
        file_name = file.display_name.encode('utf-8')

        file_path = helpers.file_path(file)
        if file_path is None:
            log.warn('No path exists for requested media file: %r', file)
            raise HTTPNotFound().exception
        file_path = file_path.encode('utf-8')

        if not os.path.exists(file_path):
            log.warn('No such file or directory: %r', file_path)
            raise HTTPNotFound().exception

        # Ensure the request accepts files with this container
        accept = request.environ.get('HTTP_ACCEPT', '*/*')
        if not mimeparse.best_match([file_type], accept):
            raise HTTPNotAcceptable().exception  # 406

        method = config.get('file_serve_method', None)
        headers = []

        # Serving files with this header breaks playback on iPhone
        if download:
            headers.append(('Content-Disposition',
                            'attachment; filename="%s"' % file_name))

        if method == 'apache_xsendfile':
            # Requires mod_xsendfile for Apache 2.x
            # XXX: Don't send Content-Length or Etag headers,
            #      Apache handles them for you.
            response.headers['X-Sendfile'] = file_path
            response.body = ''

        elif method == 'nginx_redirect':
            raise NotImplementedError, 'This is only a placeholder'
            response.headers['X-Accel-Redirect'] = '../relative/path'

        else:
            app = FileApp(file_path, headers, content_type=file_type)
            return forward(app)

        response.headers['Content-Type'] = file_type
        for header, value in headers:
            response.headers[header] = value

        return None
예제 #44
0
 def _serve_file(self, path):
     """Call Paste's FileApp (a WSGI application) to serve the file
     at the specified path
     """
     request.environ["PATH_INFO"] = "/%s" % path
     return forward(PkgResourcesParser("pylons", "pylons"))
예제 #45
0
 def _serve_file(self, path):
     """Call Paste's FileApp (a WSGI application) to serve the file
     at the specified path
     """
     request.environ['PATH_INFO'] = '/%s' % path
     return forward(PkgResourcesParser('pylons', 'pylons'))
예제 #46
0
 def _serve_file(self, path):
     """Call Paste's FileApp (a WSGI application) to serve the file
     at the specified path
     """
     return forward(paste.fileapp.FileApp(path))
예제 #47
0
 def xmlrpc(self):
     return forward(BackupXMLRPCController())
예제 #48
0
 def show(self, id, format='html'):
     'GET /scenarios/id: Show a specific item'
     # If the output format is not supported, 
     if format not in ['html', 'zip', 'geojson', 'json']: 
         return 'Unsupported output format: ' + format 
     try:
         id = int(id)
     except ValueError:
         return redirect(url('scenarios'))
     # Load
     personID = h.getPersonID()
     c.scenario = Session.query(model.Scenario).filter(model.Scenario.id==id).filter(model.getScopeFilter(personID)).first()
     # If user does not have access to the scenario,
     if not c.scenario:
         c.status = model.statusFailed
         if format == 'html':
             return render('/scenarios/show.mako')
         elif format == 'zip':
             return ''
         elif format == 'geojson':
             return geojson.dumps(geojson.FeatureCollection([]))
         elif format == 'json':
             return cjson.encode({})
     # If the scenario has an error,
     if c.scenario.status == model.statusFailed:
         c.traceback = c.scenario.output['traceback']
         c.status = model.statusFailed
         if format == 'html':
             return render('/scenarios/show.mako')
         elif format == 'zip':
             return forward(FileApp(c.scenario.getFolder() + '.zip'))
         elif format == 'geojson':
             return geojson.dumps(geojson.FeatureCollection([]))
         elif format == 'json':
             return c.scenario.exportJSON()
     # If the scenario has not been processed,
     if c.scenario.isQueued():
         c.status = model.statusPending
         if format == 'html':
             return render('/scenarios/show.mako')
         elif format == 'zip':
             return forward(FileApp(c.scenario.getFolder() + '.zip'))
         elif format == 'geojson':
             return geojson.dumps(geojson.FeatureCollection([]))
         elif format == 'json':
             return c.scenario.exportJSON()
     # Prepare
     c.status = model.statusDone
     c.scenarioInput = c.scenario.input
     c.scenarioOutput = c.scenario.output
     transform_point = geometry_store.get_transform_point(geometry_store.proj4LL, geometry_store.proj4SM)
     # If the user wants HTML,
     if format == 'html':
         # Render scenario
         c.metricModel = metric.getModel(c.scenarioInput['metric model name'])
         scenarioStatistics = c.scenarioOutput['statistics']
         nodeStatistics = scenarioStatistics['node']
         # Prepare map
         centerX, centerY = transform_point(nodeStatistics['mean longitude'], nodeStatistics['mean latitude'])
         box1X, box1Y = transform_point(nodeStatistics['minimum longitude'], nodeStatistics['maximum latitude'])
         box2X, box2Y = transform_point(nodeStatistics['maximum longitude'], nodeStatistics['minimum latitude'])
         # Render map
         datasetStore = c.scenario.getDataset()
         c.mapFeatures = datasetStore.exportGeoJSON(transform_point)
         c.mapCenter = '%s, %s' % (centerX, centerY)
         c.mapBox = '%s, %s, %s, %s' % (box1X, box1Y, box2X, box2Y)
         # Render nodes
         c.nodes = list(datasetStore.cycleNodes())
         c.populationQuartiles = scenarioStatistics['metric']['population quartiles']
         # Render scenarios
         c.scenarios = Session.query(model.Scenario).filter(model.getScopeFilter(personID)).filter(model.Scenario.status==model.statusDone).filter(model.Scenario.id!=c.scenario.id).order_by(model.Scenario.id.desc()).all()
         # Return
         return render('/scenarios/show.mako')
     elif format == 'zip':
         return forward(FileApp(c.scenario.getFolder() + '.zip'))
     elif format == 'geojson':
         return c.scenario.getDataset().exportGeoJSON(transform_point)
     elif format == 'json':
         return c.scenario.exportJSON(request.params.get('nodeID'))
예제 #49
0
 def xmlrpc(self):
     return forward(DashboardXMLRPCController())
예제 #50
0
파일: error.py 프로젝트: franoreilly/sweepy
 def _serve_file(self, path):
     """Call Paste's FileApp (a WSGI application) to serve the file
     at the specified path
     """
     request.environ['PATH_INFO'] = '/%s' % path
     return forward(PkgResourcesParser('pylons', 'pylons'))
예제 #51
0
    def serve(self, id, download=False, **kwargs):
        """Serve a :class:`~mediacore.model.media.MediaFile` binary.

        :param id: File ID
        :type id: ``int``
        :param bool download: If true, serve with an Content-Disposition that
            makes the file download to the users computer instead of playing
            in the browser.
        :raises webob.exc.HTTPNotFound: If no file exists with this ID.
        :raises webob.exc.HTTPNotAcceptable: If an Accept header field
            is present, and if the mimetype of the requested file doesn't
            match, then a 406 (not acceptable) response is returned.

        """
        file = fetch_row(MediaFile, id=id)
        request.perm.assert_permission(u'view', file.media.resource)

        file_type = file.mimetype.encode('utf-8')
        file_name = file.display_name.encode('utf-8')

        file_path = helpers.file_path(file)
        if file_path is None:
            log.warn('No path exists for requested media file: %r', file)
            raise HTTPNotFound()
        file_path = file_path.encode('utf-8')

        if not os.path.exists(file_path):
            log.warn('No such file or directory: %r', file_path)
            raise HTTPNotFound()

        # Ensure the request accepts files with this container
        accept = request.environ.get('HTTP_ACCEPT', '*/*')
        if not mimeparse.best_match([file_type], accept):
            raise HTTPNotAcceptable()  # 406

        method = config.get('file_serve_method', None)
        headers = []

        # Serving files with this header breaks playback on iPhone
        if download:
            headers.append(('Content-Disposition',
                            'attachment; filename="%s"' % file_name))

        if method == 'apache_xsendfile':
            # Requires mod_xsendfile for Apache 2.x
            # XXX: Don't send Content-Length or Etag headers,
            #      Apache handles them for you.
            response.headers['X-Sendfile'] = file_path
            response.body = ''

        elif method == 'nginx_redirect':
            # Requires NGINX server configuration:
            # NGINX must have a location block configured that matches
            # the __mediacore_serve__ path below. It should also be
            # configured as an "internal" location to prevent people from
            # surfing directly to it.
            # For more information see: http://wiki.nginx.org/XSendfile
            redirect_filename = '/__mediacore_serve__/%s' % os.path.basename(
                file_path)
            response.headers['X-Accel-Redirect'] = redirect_filename

        else:
            app = FileApp(file_path, headers, content_type=file_type)
            return forward(app)

        response.headers['Content-Type'] = file_type
        for header, value in headers:
            response.headers[header] = value

        return None