1- import asyncio , aiofiles
2- import os , tempfile
3- import json , mimetypes , re
4- from datetime import datetime
5- import logging , base64 , random
6- from io import BytesIO
7- import json , mimetypes
8- import logging , hashlib
9- from uuid import uuid1
1+ import asyncio
2+ import aiofiles
3+ import base64
4+ import hashlib
105import httpx
11- from typing import Union
12- from io import BytesIO
6+ import json
7+ import logging
8+ import mimetypes
9+ import os
10+ import random
11+ import re
12+ import tempfile
1313import uuid
14+ from urllib .parse import urlparse
15+ from datetime import datetime
1416from httpx import AsyncClient
17+ from io import BytesIO
18+ from typing import TYPE_CHECKING , Optional , Union
1519from typing import TYPE_CHECKING , Optional
20+ from swibots .api .common .models import Media
21+ from swibots .config import APP_CONFIG
22+ from swibots .errors import UnknownBackBlazeError , FileTooLarge
23+ from swibots .types import MAX_FILE_SIZE
24+
25+ from swibots .utils .types import (
26+ DownloadProgressCallback ,
27+ IOClient ,
28+ UploadProgress ,
29+ UploadProgressCallback ,
30+ )
1631
1732from swibots .errors import UnknownBackBlazeError , FileTooLarge
1833from swibots .utils .types import (
@@ -157,14 +172,18 @@ async def file_to_response(
157172 async def generate_from_ffmpeg (self , path : str , hw : int ):
158173 log .debug ("checking for ffmpeg" )
159174 ffmpeg_path = os .getenv ("FFMPEG_PATH" ) or "ffmpeg"
160- proc = await asyncio .create_subprocess_exec (
161- ffmpeg_path , stderr = asyncio .subprocess .PIPE
162- )
163- await proc .wait ()
175+ try :
176+ proc = await asyncio .create_subprocess_exec (
177+ ffmpeg_path , stderr = asyncio .subprocess .PIPE
178+ )
179+ await proc .wait ()
180+ except Exception as er :
181+ logger .error (er )
182+ return
164183 if b"ffmpeg version" not in (er := await proc .stderr .read ()):
165184 log .info (f"ffmpeg is not installed, { er } " )
166185 return
167- name = os .path .join (tempfile .gettempdir (), f"{ uuid1 ()} .png" )
186+ name = os .path .join (tempfile .gettempdir (), f"{ uuid . uuid1 ()} .png" )
168187 log .info (f"generating thumb for '{ path } '" )
169188 cmd = [
170189 ffmpeg_path ,
@@ -192,6 +211,13 @@ async def get_thumb_url(
192211 ) -> str :
193212 if not path :
194213 return
214+
215+ if path and isinstance (path , str ):
216+ # check for valid urls
217+ parse = urlparse (path )
218+ if parse .scheme and parse .netloc :
219+ return path
220+
195221 __remove = False
196222 mime_type = (
197223 mimetypes .guess_type (path .name if isinstance (path , BytesIO ) else path )[0 ]
@@ -332,8 +358,12 @@ async def upload_media(
332358 )
333359 else :
334360 path , file_response = await self .file_to_response (
335- path , mime_type , file_name , callback = callback , file_size = size ,
336- callback_args = callback_args
361+ path ,
362+ mime_type ,
363+ file_name ,
364+ callback = callback ,
365+ file_size = size ,
366+ callback_args = callback_args ,
337367 )
338368 if not file_response .get ("fileName" ):
339369 raise UnknownBackBlazeError (file_response )
@@ -357,7 +387,7 @@ async def upload_media(
357387 "sourceUri" : file_response ["fileId" ],
358388 "checksum" : file_response ["contentSha1" ],
359389 "ownerId" : self .client .app .user .id ,
360- "premium" : premium
390+ "premium" : premium ,
361391 }
362392 return self .client .build_object (Media , media )
363393
0 commit comments