[pitivi] previewers: Improved cached files naming
- From: Alexandru Băluț <alexbalut src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pitivi] previewers: Improved cached files naming
- Date: Fri, 7 Feb 2020 15:42:55 +0000 (UTC)
commit 4408f60e8d4bc20d0777d591742b79665eff9885
Author: Vivek R <123vivekr gmail com>
Date: Mon Jan 27 12:20:54 2020 +0530
previewers: Improved cached files naming
Fixes #2377
pitivi/timeline/previewers.py | 34 +++++++++++++++++++++++++++++-----
pitivi/utils/misc.py | 13 -------------
tests/test_previewers.py | 17 +++++++++++++++++
3 files changed, 46 insertions(+), 18 deletions(-)
---
diff --git a/pitivi/timeline/previewers.py b/pitivi/timeline/previewers.py
index 0660df63..531b1cf1 100644
--- a/pitivi/timeline/previewers.py
+++ b/pitivi/timeline/previewers.py
@@ -16,6 +16,7 @@
# License along with this program; if not, see <http://www.gnu.org/licenses/>.
"""Previewers for the timeline."""
import contextlib
+import hashlib
import os
import random
import sqlite3
@@ -33,7 +34,6 @@ from gi.repository import Gtk
from pitivi.settings import GlobalSettings
from pitivi.settings import xdg_cache_home
from pitivi.utils.loggable import Loggable
-from pitivi.utils.misc import hash_file
from pitivi.utils.misc import path_from_uri
from pitivi.utils.misc import quantize
from pitivi.utils.misc import quote_uri
@@ -940,8 +940,14 @@ class ThumbnailCache(Loggable):
@staticmethod
def dbfile_name(uri):
"""Returns the cache file path for the specified URI."""
- filename = hash_file(Gst.uri_get_location(uri))
- thumbs_cache_dir = xdg_cache_home("thumbs")
+ filename = gen_filename(Gst.uri_get_location(uri), "db")
+ thumbs_dir = xdg_cache_home("thumbs")
+ thumbs_cache_dir = os.path.join(thumbs_dir, "v1")
+
+ if not os.path.exists(thumbs_cache_dir):
+ os.makedirs(thumbs_cache_dir)
+ GLib.idle_add(delete_all_files_in_dir, thumbs_dir)
+
return os.path.join(thumbs_cache_dir, filename)
@classmethod
@@ -1070,12 +1076,30 @@ class ThumbnailCache(Loggable):
self.log("Saved thumbnail cache file")
+def delete_all_files_in_dir(path):
+ """Deletes the files in path without descending into subdirectories."""
+ for dir_entry in os.scandir(path):
+ if dir_entry.is_file() or dir_entry.is_symlink():
+ os.unlink(dir_entry.path)
+
+
+def gen_filename(uri, extension):
+ """Generates the cache filename for the specified URI."""
+ uri_hash = hashlib.sha256(uri.encode("UTF-8")).hexdigest()
+ return "{}_{}_{}.{}".format(os.path.basename(uri), uri_hash, os.path.getmtime(uri), extension)
+
+
def get_wavefile_location_for_uri(uri):
"""Computes the URI where the wave.npy file should be stored."""
if ProxyManager.is_proxy_asset(uri):
uri = ProxyManager.get_target_uri(uri)
- filename = hash_file(Gst.uri_get_location(uri)) + ".wave.npy"
- cache_dir = xdg_cache_home("waves")
+ filename = gen_filename(Gst.uri_get_location(uri), "wave.npy")
+ waves_dir = xdg_cache_home("waves")
+ cache_dir = os.path.join(waves_dir, "v1")
+
+ if not os.path.exists(cache_dir):
+ os.makedirs(cache_dir)
+ GLib.idle_add(delete_all_files_in_dir, waves_dir)
return os.path.join(cache_dir, filename)
diff --git a/pitivi/utils/misc.py b/pitivi/utils/misc.py
index 70f93485..ba844e76 100644
--- a/pitivi/utils/misc.py
+++ b/pitivi/utils/misc.py
@@ -15,7 +15,6 @@
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, see <http://www.gnu.org/licenses/>.
-import hashlib
import os
import subprocess
import threading
@@ -223,18 +222,6 @@ class PathWalker(Thread):
self.stopme.set()
-def hash_file(uri):
- """Hashes the first 256KB of the specified file."""
- sha256 = hashlib.sha256()
- with open(uri, "rb") as file:
- for unused in range(1024):
- chunk = file.read(256)
- if not chunk:
- break
- sha256.update(chunk)
- return sha256.hexdigest()
-
-
def quantize(value, interval):
return (value // interval) * interval
diff --git a/tests/test_previewers.py b/tests/test_previewers.py
index 0e7cbf5e..2f0dfcf3 100644
--- a/tests/test_previewers.py
+++ b/tests/test_previewers.py
@@ -25,6 +25,7 @@ from gi.repository import GdkPixbuf
from gi.repository import GES
from gi.repository import Gst
+from pitivi.timeline.previewers import delete_all_files_in_dir
from pitivi.timeline.previewers import get_wavefile_location_for_uri
from pitivi.timeline.previewers import Previewer
from pitivi.timeline.previewers import THUMB_HEIGHT
@@ -170,3 +171,19 @@ class TestThumbnailCache(BaseTestMediaLibrary):
thumb_cache = ThumbnailCache(sample_uri)
self.assertTrue(Gst.SECOND in thumb_cache)
self.assertIsNotNone(thumb_cache[Gst.SECOND])
+
+
+class TestFunctions(BaseTestMediaLibrary):
+ """Tests for the standalone functions."""
+
+ def test_delete_all_files_in_dir(self):
+ """Checks whether files in sub directories are deleted."""
+ with tempfile.TemporaryDirectory() as dir_a, \
+ tempfile.NamedTemporaryFile(dir=dir_a, delete=False), \
+ tempfile.TemporaryDirectory(dir=dir_a) as dir_a_b, \
+ tempfile.NamedTemporaryFile(dir=dir_a_b) as file_a_b1:
+ self.assertEqual(len(os.listdir(dir_a)), 2)
+ self.assertEqual(len(os.listdir(dir_a_b)), 1)
+ delete_all_files_in_dir(dir_a)
+ self.assertEqual(os.listdir(dir_a), [os.path.basename(dir_a_b)])
+ self.assertEqual(os.listdir(dir_a_b), [os.path.basename(file_a_b1.name)])
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]