def _InitTempDir( self ): self.temp_dir = HydrusPaths.GetTempDir()
def GetFileInfo(path, mime=None, ok_to_look_for_hydrus_updates=False): size = os.path.getsize(path) if size == 0: raise HydrusExceptions.FileSizeException('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.UnsupportedFileException( 'Looks like HTML -- maybe the client needs to be taught how to parse this?' ) elif mime == HC.APPLICATION_UNKNOWN: raise HydrusExceptions.UnsupportedFileException( 'Unknown filetype!') else: raise HydrusExceptions.UnsupportedFileException( '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_ZIP: temp_dir_path = HydrusPaths.GetTempDir() try: subprocess.call(["unzip", path, '-d', temp_dir_path]) cover = sorted(list(Path(temp_dir_path).rglob("*.jpg")) + list(Path(temp_dir_path).rglob("*.png")), key=lambda p: p.name)[0].as_posix() ((width, height), duration, num_frames) = HydrusImageHandling.GetImageProperties( cover, HC.IMAGE_PNG) # TODO: delete dir except Exception as e: (width, height, duration, num_frames) = (300, 300, 0, 0) elif mime == HC.APPLICATION_RAR: temp_dir_path = HydrusPaths.GetTempDir() try: subprocess.call(["unrar", path, temp_dir_path]) cover = sorted(list(Path(temp_dir_path).rglob("*.jpg")) + list(Path(temp_dir_path).rglob("*.png")), key=lambda p: p.name)[0].as_posix() ((width, height), duration, num_frames) = HydrusImageHandling.GetImageProperties( cover, HC.IMAGE_PNG) except Exception as e: (width, height, duration, num_frames) = (300, 300, 0, 0) 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)
def GenerateThumbnailBytes(path, target_resolution, mime, duration, num_frames, percentage_in=35): if mime in (HC.IMAGE_JPEG, HC.IMAGE_PNG, HC.IMAGE_GIF, HC.IMAGE_WEBP, HC.IMAGE_TIFF, HC.IMAGE_ICON): # not apng atm thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( path, target_resolution, mime) elif mime in [HC.APPLICATION_ZIP]: temp_dir_path = HydrusPaths.GetTempDir() try: cmd = ["unzip", path, '-d', temp_dir_path] subprocess.call(cmd) cover = sorted(list(Path(temp_dir_path).rglob("*.jpg")) + list(Path(temp_dir_path).rglob("*.png")), key=lambda p: p.name)[0].as_posix() thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( cover, target_resolution, mime) except Exception as e: thumb_path = os.path.join(HC.STATIC_DIR, 'zip.png') thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( thumb_path, target_resolution, mime) elif mime in [HC.APPLICATION_RAR]: temp_dir_path = HydrusPaths.GetTempDir() try: cmd = ["unrar", 'x', path, temp_dir_path] subprocess.call(cmd) cover = sorted(list(Path(temp_dir_path).rglob("*.jpg")) + list(Path(temp_dir_path).rglob("*.png")), key=lambda p: p.name)[0].as_posix() thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( cover, target_resolution, mime) except Exception as e: thumb_path = os.path.join(HC.STATIC_DIR, 'rar.png') thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( thumb_path, target_resolution, mime) else: if mime == HC.APPLICATION_FLASH: (os_file_handle, temp_path) = HydrusPaths.GetTempPath() try: HydrusFlashHandling.RenderPageToFile(path, temp_path, 1) thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( temp_path, target_resolution, mime) except: thumb_path = os.path.join(HC.STATIC_DIR, 'flash.png') thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesFromStaticImagePath( thumb_path, target_resolution, mime) finally: HydrusPaths.CleanUpTempPath(os_file_handle, temp_path) else: renderer = HydrusVideoHandling.VideoRendererFFMPEG( path, mime, duration, num_frames, target_resolution) renderer.read_frame( ) # this initialises the renderer and loads the first frame as a fallback desired_thumb_frame = int((percentage_in / 100.0) * num_frames) renderer.set_position(desired_thumb_frame) numpy_image = renderer.read_frame() if numpy_image is None: raise Exception( 'Could not create a thumbnail from that video!') numpy_image = HydrusImageHandling.ResizeNumPyImage( numpy_image, target_resolution) # just in case ffmpeg doesn't deliver right thumbnail_bytes = HydrusImageHandling.GenerateThumbnailBytesNumPy( numpy_image, mime) renderer.Stop() del renderer return thumbnail_bytes
def test_import_folders_daemon(self): test_dir = HydrusPaths.GetTempDir() try: HG.test_controller.SetRead('hash_status', (CC.STATUS_UNKNOWN, None, '')) HydrusPaths.MakeSureDirectoryExists(test_dir) hydrus_png_path = os.path.join(HC.STATIC_DIR, 'hydrus.png') HydrusPaths.MirrorFile(hydrus_png_path, os.path.join(test_dir, '0')) HydrusPaths.MirrorFile(hydrus_png_path, os.path.join(test_dir, '1')) # previously imported HydrusPaths.MirrorFile(hydrus_png_path, os.path.join(test_dir, '2')) with open(os.path.join(test_dir, '3'), 'wb') as f: f.write(b'blarg') # broken with open(os.path.join(test_dir, '4'), 'wb') as f: f.write(b'blarg') # previously failed # actions = {} actions[CC.STATUS_SUCCESSFUL_AND_NEW] = CC.IMPORT_FOLDER_DELETE actions[ CC.STATUS_SUCCESSFUL_BUT_REDUNDANT] = CC.IMPORT_FOLDER_DELETE actions[CC.STATUS_DELETED] = CC.IMPORT_FOLDER_DELETE actions[CC.STATUS_ERROR] = CC.IMPORT_FOLDER_IGNORE import_folder = ClientImportLocal.ImportFolder('imp', path=test_dir, actions=actions) HG.test_controller.SetRead('serialisable_names', ['imp']) HG.test_controller.SetRead('serialisable_named', import_folder) HG.test_controller.ClearWrites('import_file') HG.test_controller.ClearWrites('serialisable') ClientDaemons.DAEMONCheckImportFolders() import_file = HG.test_controller.GetWrite('import_file') self.assertEqual(len(import_file), 3) # I need to expand tests here with the new file system [((updated_import_folder, ), empty_dict) ] = HG.test_controller.GetWrite('serialisable') self.assertEqual(updated_import_folder, import_folder) self.assertTrue(not os.path.exists(os.path.join(test_dir, '0'))) self.assertTrue(not os.path.exists(os.path.join(test_dir, '1'))) self.assertTrue(not os.path.exists(os.path.join(test_dir, '2'))) self.assertTrue(os.path.exists(os.path.join(test_dir, '3'))) self.assertTrue(os.path.exists(os.path.join(test_dir, '4'))) finally: shutil.rmtree(test_dir)