Exemplo n.º 1
0
def CheckTagNotEmpty( tag ):
    
    ( namespace, subtag ) = SplitTag( tag )
    
    if subtag == '':
        
        raise HydrusExceptions.SizeException( 'Received a zero-length tag!' )
Exemplo n.º 2
0
def GetMime(path, ok_to_look_for_hydrus_updates=False):

    size = os.path.getsize(path)

    if size == 0:

        raise HydrusExceptions.SizeException('File is of zero length!')

    with open(path, 'rb') as f:

        bit_to_check = f.read(256)

    for (offset, header, mime) in header_and_mime:

        offset_bit_to_check = bit_to_check[offset:]

        if offset_bit_to_check.startswith(header):

            if mime == HC.UNDETERMINED_WM:

                if HydrusVideoHandling.HasVideoStream(path):

                    return HC.VIDEO_WMV

                # we'll catch and verify wma later

            elif mime == HC.UNDETERMINED_PNG:

                if HydrusVideoHandling.HasVideoStream(path):

                    return HC.IMAGE_APNG

                else:

                    return HC.IMAGE_PNG

            else:

                return mime

    try:

        mime = HydrusVideoHandling.GetMime(path)

        if mime != HC.APPLICATION_UNKNOWN:

            return mime

    except HydrusExceptions.MimeException:

        pass

    except Exception as e:

        HydrusData.Print('FFMPEG had trouble with: ' + path)
        HydrusData.PrintException(e, do_wait=False)

    if HydrusText.LooksLikeHTML(bit_to_check):

        return HC.TEXT_HTML

    if ok_to_look_for_hydrus_updates:

        with open(path, 'rb') as f:

            update_network_bytes = f.read()

        try:

            update = HydrusSerialisable.CreateFromNetworkBytes(
                update_network_bytes)

            if isinstance(update, HydrusNetwork.ContentUpdate):

                return HC.APPLICATION_HYDRUS_UPDATE_CONTENT

            elif isinstance(update, HydrusNetwork.DefinitionsUpdate):

                return HC.APPLICATION_HYDRUS_UPDATE_DEFINITIONS

        except:

            pass

    return HC.APPLICATION_UNKNOWN
Exemplo n.º 3
0
def GetFileInfo(path, mime=None, ok_to_look_for_hydrus_updates=False):

    size = os.path.getsize(path)

    if size == 0:

        raise HydrusExceptions.SizeException('File is of zero length!')

    if mime is None:

        mime = GetMime(
            path, ok_to_look_for_hydrus_updates=ok_to_look_for_hydrus_updates)

    if mime not in HC.ALLOWED_MIMES:

        if mime == HC.TEXT_HTML:

            raise HydrusExceptions.MimeException(
                'Looks like HTML -- maybe the client needs to be taught how to parse this?'
            )

        elif mime == HC.APPLICATION_UNKNOWN:

            raise HydrusExceptions.MimeException('Unknown filetype!')

        else:

            raise HydrusExceptions.MimeException('Filetype is not permitted!')

    width = None
    height = None
    duration = None
    num_frames = None
    num_words = None

    if mime in (HC.IMAGE_JPEG, HC.IMAGE_PNG, HC.IMAGE_GIF, HC.IMAGE_WEBP,
                HC.IMAGE_TIFF, HC.IMAGE_ICON):

        ((width, height), duration,
         num_frames) = HydrusImageHandling.GetImageProperties(path, mime)

    elif mime == HC.APPLICATION_FLASH:

        ((width, height), duration,
         num_frames) = HydrusFlashHandling.GetFlashProperties(path)

    elif mime in (HC.IMAGE_APNG, HC.VIDEO_AVI, HC.VIDEO_FLV, HC.VIDEO_WMV,
                  HC.VIDEO_MOV, HC.VIDEO_MP4, HC.VIDEO_MKV, HC.VIDEO_REALMEDIA,
                  HC.VIDEO_WEBM, HC.VIDEO_MPEG):

        ((width, height), duration,
         num_frames) = HydrusVideoHandling.GetFFMPEGVideoProperties(path)

    elif mime == HC.APPLICATION_PDF:

        num_words = HydrusDocumentHandling.GetPDFNumWords(
            path)  # this now give None until a better solution can be found

    elif mime == HC.APPLICATION_PSD:

        (width, height) = HydrusImageHandling.GetPSDResolution(path)

    elif mime in HC.AUDIO:

        ffmpeg_lines = HydrusVideoHandling.GetFFMPEGInfoLines(path)

        (file_duration_in_s, stream_duration_in_s
         ) = HydrusVideoHandling.ParseFFMPEGDuration(ffmpeg_lines)

        duration = int(file_duration_in_s * 1000)

    if mime in HC.MIMES_THAT_DEFINITELY_HAVE_AUDIO:

        has_audio = True

    elif mime in HC.MIMES_THAT_MAY_HAVE_AUDIO:

        has_audio = HydrusAudioHandling.VideoHasAudio(path)

    else:

        has_audio = False

    if width is not None and width < 0:

        width *= -1

    if height is not None and height < 0:

        width *= -1

    if duration is not None and duration < 0:

        duration *= -1

    if num_frames is not None and num_frames < 0:

        num_frames *= -1

    if num_words is not None and num_words < 0:

        num_words *= -1

    return (size, mime, width, height, duration, num_frames, has_audio,
            num_words)