python类MimeTypes()的实例源码

utils.py 文件源码 项目:speech-to-text 作者: rmotr 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def guess_mime_type(audio_file_path, forced_mime_type=None):
    mime = MimeTypes()
    mime_type = forced_mime_type or mime.guess_type(audio_file_path)[0]
    if mime_type not in SUPPORTED_CONTENT_TYPES:
        raise ValueError('MimeType {} not recognized or supported for '
                         'audio file {}'.format(mime_type, audio_file_path))
    return mime_type
api.py 文件源码 项目:exchange 作者: boundlessgeo 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def view(self, request, **kwargs):
        '''
        allow a file to be viewed as opposed to download. This is particularly needed when a video file is stored
        in the fileservice and user wants to be able to use a view the video as opposed to having to download it
        first. It passes the serving of the file to nginx/apache which will return all the proper headers allowing,
        say, html5's video viewer's 'seek' indicator/knob to work. Otherwise the video is only played sequentially

        Note that nginx/apache need to be configured accordingly. nginx for example:
        location /var/lib/geoserver_data/file-service-store/ {
           # forces requests to be authorized
           internal;
           alias   /var/lib/geoserver_data/file-service-store/;
        }

        for apache, need to install xsendfile module, enable it, set the path and then
        XSendFile on
        XSendFilePath /var/lib/geoserver_data/file-service-store

        example use:
        /fileservice/view/med.mp4
        or
        /fileservice/med.mp4/view

        Note that media players tend to require the route to end with the filename like /fileservice/view/med.mp4
        '''

        # method check to avoid bad requests
        self.method_check(request, allowed=['get'])
        # Must be done otherwise endpoint will be wide open
        self.is_authenticated(request)

        file_item_name = kwargs.get('name', None)
        url = urllib.pathname2url(file_item_name)
        mime = MimeTypes()
        mime_type = mime.guess_type(url)
        response = HttpResponse(content_type=mime_type[0])
        file_with_route = smart_str('{}{}'.format(helpers.get_fileservice_dir(), file_item_name))
        # apache header
        response['X-Sendfile'] = file_with_route
        # nginx header
        response['X-Accel-Redirect'] = file_with_route

        return response
fab_ui_case_study_images.py 文件源码 项目:directory-tests 作者: uktrade 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def prepare_form_data(token: str, case_study: CaseStudy) -> (dict, dict):
    """Prepare form data based on the flags and custom values provided.

    :param token: CSRF middleware token required to submit the form
    :param case_study: a CaseStudy namedtuple with random data
    :return: a tuple consisting of form data and files to upload
    """
    data = {
        "csrfmiddlewaretoken": token,
        "supplier_case_study_wizard_view-current_step": "rich-media",
        "rich-media-image_one_caption": case_study.caption_1,
        "rich-media-image_two_caption": case_study.caption_2,
        "rich-media-image_three_caption": case_study.caption_3,
        "rich-media-testimonial": case_study.testimonial,
        "rich-media-testimonial_name": case_study.source_name,
        "rich-media-testimonial_job_title": case_study.source_job,
        "rich-media-testimonial_company": case_study.source_company
    }

    paths = [case_study.image_1, case_study.image_2, case_study.image_3]

    def get_basename(path):
        return basename(path) if path is not None else ""

    def get_mimetype(path):
        return MimeTypes().guess_type(path)[0] if path is not None else ""

    def read_image(path: str):
        res = ""
        if path is not None:
            with open(path, "rb") as f:
                res = f.read()
        return res

    name_1, name_2, name_3 = (get_basename(path) for path in paths)
    mime_1, mime_2, mime_3 = (get_mimetype(path) for path in paths)
    file_1, file_2, file_3 = (read_image(path) for path in paths)
    files = {
        "rich-media-image_one": (name_1, file_1, mime_1),
        "rich-media-image_two": (name_2, file_2, mime_2),
        "rich-media-image_three": (name_3, file_3, mime_3),
    }

    return data, files


问题


面经


文章

微信
公众号

扫码关注公众号