mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-30 11:56:18 -05:00
Removing python-magic requirement in favor of file command.
This commit is contained in:
parent
fa83728f52
commit
776ac7edb0
4 changed files with 55 additions and 14 deletions
|
|
@ -1,11 +1,11 @@
|
|||
import pathlib
|
||||
from typing import List
|
||||
from typing import List, Callable
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_files_structure(test_directory):
|
||||
def make_files_structure(test_directory) -> Callable[[List[str]], List[pathlib.Path]]:
|
||||
def create_files(paths: List[str]) -> List[pathlib.Path]:
|
||||
files = []
|
||||
for name in paths:
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import asyncio
|
||||
import re
|
||||
import subprocess
|
||||
from functools import wraps
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
|
@ -9,23 +10,16 @@ import psycopg2
|
|||
from sqlalchemy.orm import Session
|
||||
|
||||
from modules.files.models import File
|
||||
from wrolpi.cmd import which
|
||||
from wrolpi.common import get_media_directory, wrol_mode_check, walk, chunks, logger
|
||||
from wrolpi.dates import from_timestamp
|
||||
from wrolpi.db import get_db_session, get_db_curs, get_ranked_models
|
||||
from wrolpi.errors import InvalidFile
|
||||
from wrolpi.vars import PYTEST
|
||||
|
||||
try:
|
||||
import magic
|
||||
|
||||
have_magic = True
|
||||
except ImportError:
|
||||
logger.error('Could not import magic. Is libmagic installed?')
|
||||
have_magic = False
|
||||
|
||||
logger = logger.getChild(__name__)
|
||||
|
||||
__all__ = ['list_files', 'delete_file', 'split_file_name', 'upsert_file', 'refresh_files', 'search']
|
||||
__all__ = ['list_files', 'delete_file', 'split_file_name', 'upsert_file', 'refresh_files', 'search', 'get_mimetype']
|
||||
|
||||
|
||||
def filter_parent_directories(directories: List[Path]) -> List[Path]:
|
||||
|
|
@ -97,14 +91,26 @@ def split_file_name(path: Path) -> List[str]:
|
|||
return words
|
||||
|
||||
|
||||
FILE_PATH = which('file', '/usr/bin/file')
|
||||
|
||||
|
||||
def get_mimetype(path: Path) -> str:
|
||||
"""Get the mimetype using the builtin `file` command."""
|
||||
cmd = (FILE_PATH, '--mime-type', str(path.absolute()))
|
||||
output = subprocess.check_output(cmd)
|
||||
output = output.decode()
|
||||
mimetype = output.split(' ')[-1].strip()
|
||||
return mimetype
|
||||
|
||||
|
||||
def upsert_file(path: Path, session: Session) -> File:
|
||||
"""Update/insert a File in the DB. Gather metadata about it."""
|
||||
file = session.query(File).filter_by(path=path).one_or_none()
|
||||
if not file:
|
||||
file = File(path=path)
|
||||
session.add(file)
|
||||
if not file.mimetype and have_magic:
|
||||
file.mimetype = magic.from_file(str(path), mime=True)
|
||||
if not file.mimetype:
|
||||
file.mimetype = get_mimetype(path)
|
||||
if not file.title:
|
||||
file.title = split_file_name(path)
|
||||
if not file.size or not file.modification_datetime:
|
||||
|
|
|
|||
|
|
@ -1,12 +1,20 @@
|
|||
import shutil
|
||||
from pathlib import Path
|
||||
from typing import List, Iterable
|
||||
|
||||
import pytest
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from modules.files import lib
|
||||
from modules.files.models import File
|
||||
from wrolpi.errors import InvalidFile
|
||||
from wrolpi.media_path import MediaPathType
|
||||
from wrolpi.vars import PROJECT_DIR
|
||||
|
||||
|
||||
def assert_files(session: Session, expected):
|
||||
files = {str(i.path.relative) for i in session.query(File).all()}
|
||||
assert files == set(expected)
|
||||
|
||||
|
||||
def test_list_files(make_files_structure, test_directory):
|
||||
|
|
@ -169,3 +177,31 @@ def test_refresh_files(test_session, make_files_structure, test_directory):
|
|||
lib.refresh_files()
|
||||
results = test_session.query(File)
|
||||
assert get_relative_strs(results) == []
|
||||
|
||||
|
||||
def test_mime_type(test_session, make_files_structure, test_directory):
|
||||
"""Files module uses the `file` command to get the mimetype of each file."""
|
||||
from PIL import Image
|
||||
|
||||
foo, bar, baz, empty = make_files_structure([
|
||||
'dir/foo text.txt',
|
||||
'dir/bar.jpeg',
|
||||
'dir/baz.mp4',
|
||||
'dir/empty',
|
||||
])
|
||||
foo.write_text('some text')
|
||||
Image.new('RGB', (25, 25), color='grey').save(bar)
|
||||
shutil.copy(PROJECT_DIR / 'test/big_buck_bunny_720p_1mb.mp4', baz)
|
||||
|
||||
lib.refresh_files()
|
||||
assert_files(test_session, ['dir/foo text.txt', 'dir/bar.jpeg', 'dir/baz.mp4', 'dir/empty'])
|
||||
|
||||
foo = test_session.query(File).filter_by(path=f'{test_directory}/dir/foo text.txt').one()
|
||||
bar = test_session.query(File).filter_by(path=f'{test_directory}/dir/bar.jpeg').one()
|
||||
baz = test_session.query(File).filter_by(path=f'{test_directory}/dir/baz.mp4').one()
|
||||
empty = test_session.query(File).filter_by(path=f'{test_directory}/dir/empty').one()
|
||||
|
||||
assert foo.mimetype == 'text/plain'
|
||||
assert bar.mimetype == 'image/jpeg'
|
||||
assert baz.mimetype == 'video/mp4'
|
||||
assert empty.mimetype == 'inode/x-empty'
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ psycopg2-binary==2.9.3
|
|||
pytest-asyncio==0.17.2
|
||||
pytest==6.2.5
|
||||
python-dotenv==0.19.2
|
||||
python-magic==0.4.24
|
||||
pytz==2021.3
|
||||
PyYAML==5.3.1 # this is the only version available on RPi
|
||||
requests==2.27.1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue