File size: 1,525 Bytes
b5df735
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
File service for handling file operations
"""

import aiofiles
from pathlib import Path
from typing import Optional


class FileService:
    """Service for file operations"""
    
    async def write_text_file(self, file_path: str, content: str, encoding: str = "utf-8"):
        """Write text content to file asynchronously"""
        async with aiofiles.open(file_path, 'w', encoding=encoding) as f:
            await f.write(content)
    
    async def read_text_file(self, file_path: str, encoding: str = "utf-8") -> str:
        """Read text content from file asynchronously"""
        async with aiofiles.open(file_path, 'r', encoding=encoding) as f:
            return await f.read()
    
    def ensure_directory(self, directory_path: str):
        """Ensure directory exists"""
        Path(directory_path).mkdir(parents=True, exist_ok=True)
    
    def file_exists(self, file_path: str) -> bool:
        """Check if file exists"""
        return Path(file_path).exists()
    
    def get_file_size(self, file_path: str) -> int:
        """Get file size in bytes"""
        return Path(file_path).stat().st_size
    
    def get_file_extension(self, file_path: str) -> str:
        """Get file extension"""
        return Path(file_path).suffix.lower()
    
    def is_audio_file(self, file_path: str) -> bool:
        """Check if file is an audio file"""
        audio_extensions = {'.mp3', '.wav', '.m4a', '.flac', '.aac', '.ogg', '.wma'}
        return self.get_file_extension(file_path) in audio_extensions