Ejemplo n.º 1
0
 def _ImportPngs( self, paths ):
     
     for path in paths:
         
         try:
             
             payload = ClientSerialisable.LoadFromPng( path )
             
         except Exception as e:
             
             QW.QMessageBox.critical( self, 'Error', str(e) )
             
             return
             
         
         try:
             
             obj = HydrusSerialisable.CreateFromNetworkBytes( payload )
             
             self._ImportObject( obj )
             
         except:
             
             QW.QMessageBox.critical( self, 'Error', 'I could not understand what was encoded in the file!' )
             
             return
    def _dump_and_load_and_test(self, obj, test_func):

        serialisable_tuple = obj.GetSerialisableTuple()

        self.assertIsInstance(serialisable_tuple, tuple)

        if isinstance(obj, HydrusSerialisable.SerialisableBaseNamed):

            (serialisable_type, name, version,
             serialisable_info) = serialisable_tuple

        elif isinstance(obj, HydrusSerialisable.SerialisableBase):

            (serialisable_type, version,
             serialisable_info) = serialisable_tuple

        self.assertEqual(serialisable_type, obj.SERIALISABLE_TYPE)
        self.assertEqual(version, obj.SERIALISABLE_VERSION)

        dupe_obj = HydrusSerialisable.CreateFromSerialisableTuple(
            serialisable_tuple)

        self.assertIsNot(obj, dupe_obj)

        test_func(obj, dupe_obj)

        #

        json_string = obj.DumpToString()

        self.assertIsInstance(json_string, str)

        dupe_obj = HydrusSerialisable.CreateFromString(json_string)

        self.assertIsNot(obj, dupe_obj)

        test_func(obj, dupe_obj)

        #

        network_bytes = obj.DumpToNetworkBytes()

        self.assertIsInstance(network_bytes, bytes)

        dupe_obj = HydrusSerialisable.CreateFromNetworkBytes(network_bytes)

        self.assertIsNot(obj, dupe_obj)

        test_func(obj, dupe_obj)
Ejemplo n.º 3
0
    def _ImportPNGs(self, paths):

        have_shown_load_error = False

        for path in paths:

            try:

                payload = ClientSerialisable.LoadFromPNG(path)

            except Exception as e:

                QW.QMessageBox.critical(self, 'Error', str(e))

                return

            try:

                obj = HydrusSerialisable.CreateFromNetworkBytes(
                    payload, raise_error_on_future_version=True)

                self._ImportObject(obj)

            except HydrusExceptions.SerialisationException as e:

                if not have_shown_load_error:

                    message = str(e)

                    if len(paths) > 1:

                        message += os.linesep * 2
                        message += 'If there are more objects in this import with similar load problems, they will now be skipped silently.'

                    QW.QMessageBox.critical(self, 'Problem loading', str(e))

                    have_shown_load_error = True

            except:

                QW.QMessageBox.critical(
                    self, 'Error',
                    'I could not understand what was encoded in "{}"!'.format(
                        path))

                return
Ejemplo n.º 4
0
def GetMime(path, ok_to_look_for_hydrus_updates=False):

    size = os.path.getsize(path)

    if size == 0:

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

    if ok_to_look_for_hydrus_updates and size < 64 * 1024 * 1024:

        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

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

        bit_to_check = f.read(256)

    for (offsets_and_headers, mime) in headers_and_mime:

        it_passes = False not in (bit_to_check[offset:].startswith(header)
                                  for (offset, header) in offsets_and_headers)

        if it_passes:

            if mime in (HC.UNDETERMINED_WM, HC.UNDETERMINED_MP4):

                return HydrusVideoHandling.GetMime(path)

            elif mime == HC.UNDETERMINED_PNG:

                if IsPNGAnimated(bit_to_check):

                    return HC.IMAGE_APNG

                else:

                    return HC.IMAGE_PNG

            else:

                return mime

    if HydrusText.LooksLikeHTML(bit_to_check):

        return HC.TEXT_HTML

    # it is important this goes at the end, because ffmpeg has a billion false positives!
    # for instance, it once thought some hydrus update files were mpegs
    try:

        mime = HydrusVideoHandling.GetMime(path)

        if mime != HC.APPLICATION_UNKNOWN:

            return mime

    except HydrusExceptions.UnsupportedFileException:

        pass

    except Exception as e:

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

    return HC.APPLICATION_UNKNOWN
Ejemplo n.º 5
0
def ParseNetworkBytesToParsedHydrusArgs(network_bytes):

    if len(network_bytes) == 0:

        return HydrusSerialisable.SerialisableDictionary()

    args = HydrusSerialisable.CreateFromNetworkBytes(network_bytes)

    if not isinstance(args, dict):

        raise HydrusExceptions.BadRequestException(
            'The given parameter did not seem to be a JSON Object!')

    args = ParsedRequestArguments(args)

    for param_name in BYTE_PARAMS:

        if param_name in args:

            args[param_name] = bytes.fromhex(args[param_name])

    for param_name in JSON_BYTE_LIST_PARAMS:

        if param_name in args:

            args[param_name] = [
                bytes.fromhex(encoded_item)
                for encoded_item in args[param_name]
            ]

    # account_types should be a serialisable list, so it just works

    if 'account' in args:

        args[
            'account'] = HydrusNetwork.Account.GenerateAccountFromSerialisableTuple(
                args['account'])

    if 'accounts' in args:

        account_tuples = args['accounts']

        args['accounts'] = [
            HydrusNetwork.Account.GenerateAccountFromSerialisableTuple(
                account_tuple) for account_tuple in account_tuples
        ]

    if 'service_keys_to_access_keys' in args:

        args['service_keys_to_access_keys'] = {
            bytes.fromhex(encoded_service_key):
            bytes.fromhex(encoded_access_key)
            for (encoded_service_key,
                 encoded_access_key) in args['service_keys_to_access_keys']
        }

    if 'services' in args:

        service_tuples = args['services']

        args['services'] = [
            HydrusNetwork.GenerateServiceFromSerialisableTuple(service_tuple)
            for service_tuple in service_tuples
        ]

    return args
Ejemplo n.º 6
0
def GetMime(path, ok_to_look_for_hydrus_updates=False):

    size = os.path.getsize(path)

    if size == 0:

        raise HydrusExceptions.FileSizeException('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.UnsupportedFileException:

        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