Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
KazukiPrzyborowski authored Jun 18, 2024
1 parent 78e250f commit e425592
Showing 1 changed file with 128 additions and 9 deletions.
137 changes: 128 additions & 9 deletions pycatfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@
except ImportError:
pass;

# HTTPX support
havehttpx = False;
try:
import httpx;
havehttpx = True;
logging.getLogger("httpx").setLevel(logging.WARNING);
except ImportError:
pass;

# HTTP and URL parsing
try:
from urllib.request import Request, build_opener, HTTPBasicAuthHandler;
Expand All @@ -165,6 +174,13 @@
__use_alt_format__ = False;
if(not havepysftp):
__use_pysftp__ = False;
__use_http_lib__ = "httpx";
if(__use_http_lib__=="httpx" haverequests and not havehttpx):
__use_http_lib__ = "requests";
if(__use_http_lib__=="requests" havehttpx and not haverequests):
__use_http_lib__ = "httpx";
if((__use_http_lib__=="httpx" or __use_http_lib__=="requests") and not havehttpx and not haverequests):
__use_http_lib__ = "urllib";
if(not __use_alt_format__):
''' Format Info by Kazuki Przyborowski '''
__file_format_name__ = "CatFile";
Expand Down Expand Up @@ -987,6 +1003,97 @@ def TarFileCheck(infile):
except tarfile.TarError:
return False;

def TarFileCheckAlt(infile):
try:
if is_tarfile(infile):
return True;
except TypeError:
pass;
try:
# Check if the input is a file-like object
if hasattr(infile, 'read'):
# Save the current file position
current_position = infile.tell();
# Attempt to open the file object as a tar file
with tarfile.open(fileobj=infile) as tar:
pass;
# Restore the file position
infile.seek(current_position);
else:
# Assume it's a filename and attempt to open it as a tar file
with tarfile.open(name=infile) as tar:
pass;
return True;
except (tarfile.TarError, AttributeError, IOError):
return False;

def ZipFileCheck(infile):
try:
if zipfile.is_zipfile(infile):
return True;
except TypeError:
pass;
try:
# Check if the input is a file-like object
if hasattr(infile, 'read'):
# Save the current file position
current_position = infile.tell();
# Attempt to open the file object as a zip file
with zipfile.ZipFile(infile) as zipf:
pass;
# Restore the file position
infile.seek(current_position);
else:
# Assume it's a filename and attempt to open it as a zip file
with zipfile.ZipFile(infile) as zipf:
pass;
return True;
except (zipfile.BadZipFile, AttributeError, IOError):
return False;

def RarFileCheck(infile):
try:
if rarfile.is_rarfile(infile):
return True;
except TypeError:
pass;
try:
# Check if the input is a file-like object
if hasattr(infile, 'read'):
# Save the current file position
current_position = infile.tell();
# Attempt to open the file object as a rar file
with rarfile.RarFile(infile) as rarf:
pass;
# Restore the file position
infile.seek(current_position);
else:
# Assume it's a filename and attempt to open it as a rar file
with rarfile.RarFile(infile) as rarf:
pass;
return True;
except (rarfile.Error, AttributeError, IOError):
return False;

def SevenZipFileCheck(infile):
try:
# Check if the input is a file-like object
if hasattr(infile, 'read'):
# Save the current file position
current_position = infile.tell();
# Attempt to open the file object as a 7z file
with py7zr.SevenZipFile(infile, 'r') as archive:
pass;
# Restore the file position
infile.seek(current_position);
else:
# Assume it's a filename and attempt to open it as a 7z file
with py7zr.SevenZipFile(infile, 'r') as archive:
pass;
return True;
except (py7zr.Bad7zFile, AttributeError, IOError):
return False;

# initial_value can be 0xFFFF or 0x0000
def crc16_ansi(msg, initial_value=0xFFFF):
# CRC-16-IBM / CRC-16-ANSI polynomial and initial value
Expand Down Expand Up @@ -7756,34 +7863,46 @@ def upload_file_to_ftp_string(ftpstring, url):
ftpfileo.close();
return ftpfile;

def download_file_from_http_file(url, headers=geturls_headers_pycatfile_python_alt):
def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__):
if headers is None:
headers = {};
# Parse the URL to extract username and password if present
urlparts = urlparse(url);
username = urlparts.username;
password = urlparts.password;
# Rebuild the URL without the username and password
netloc = urlparts.hostname;
if(urlparts.scheme=="sftp"):
if(__use_pysftp__):
if urlparts.scheme == "sftp":
if __use_pysftp__:
return download_file_from_pysftp_file(url);
else:
return download_file_from_sftp_file(url);
elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
elif urlparts.scheme == "ftp" or urlparts.scheme == "ftps":
return download_file_from_ftp_file(url);
if urlparts.port:
netloc += ':' + str(urlparts.port);
netloc += ':' + str(urlparts.port)
rebuilt_url = urlunparse((urlparts.scheme, netloc, urlparts.path, urlparts.params, urlparts.query, urlparts.fragment));
# Create a temporary file object
httpfile = BytesIO();
if haverequests:
# Use the requests library if available
if usehttp == 'requests' and haverequests:
# Use the requests library if selected and available
if username and password:
response = requests.get(rebuilt_url, headers=headers, auth=(username, password), stream=True);
else:
response = requests.get(rebuilt_url, headers=headers, stream=True);
response.raw.decode_content = True
response.raw.decode_content = True;
shutil.copyfileobj(response.raw, httpfile);
elif usehttp == 'httpx' and havehttpx:
# Use httpx if selected and available
with httpx.Client() as client:
if username and password:
response = client.get(rebuilt_url, headers=headers, auth=(username, password), stream=True);
else:
response = client.get(rebuilt_url, headers=headers, stream=True);
for chunk in response.iter_bytes():
httpfile.write(chunk);
else:
# Use urllib as a fallback
# Build a Request object for urllib
request = Request(rebuilt_url, headers=headers);
# Create an opener object for handling URLs
Expand All @@ -7797,7 +7916,7 @@ def download_file_from_http_file(url, headers=geturls_headers_pycatfile_python_a
# Build the opener with the authentication handler
opener = build_opener(auth_handler);
else:
opener = build_opener();
opener = build_opener();
with opener.open(request) as response:
shutil.copyfileobj(response, httpfile);
# Reset file pointer to the start
Expand Down

0 comments on commit e425592

Please sign in to comment.