Update fileparse.py
upgraded fileparser, now uses an edited version of the current session
import filetype
-import requests
-import os
+
+from ..RESTapiwrap import *
from ..logger import * #imports LogLevel and Logger
@@ -11,15 +11,15 @@ except ImportError:
class Fileparse(object):
def __init__(self, s, log): #s is the requests session object
- self.s = s
self.log = log
+ self.editedS = Wrapper.editedReqSession(s, {"remove": ["Authorization", "X-Fingerprint", "X-Super-Properties"]})
def parse(self, filelocation, isurl): #returns mimetype and extension if detected
fd = b""
if isurl:
result = urlparse(filelocation)
- if all([result.scheme, result.netloc, result.path]): #if a link...
- fd = requests.get(filelocation,headers={"User-Agent":self.s.headers['User-Agent']},proxies=self.s.proxies).content
+ if all([result.scheme, result.netloc]): #if a link...
+ fd = Wrapper.sendRequest(self.editedS, 'get', filelocation, log=self.log).content
kind = filetype.guess(fd)
if kind is None:
Logger.log('Unsupported file type. Will attempt to send anyways.', LogLevel.WARNING, self.log)
@@ -29,12 +29,12 @@ class Fileparse(object):
Logger.log('Invalid link.', LogLevel.WARNING, self.log)
return 'invalid', 'invalid', fd
else:
- if os.path.isfile(filelocation):
+ try:
kind = filetype.guess(filelocation)
if kind is None:
Logger.log('Unsupported file type. Will attempt to send anyways.', LogLevel.WARNING, self.log)
return 'unsupported', 'unsupported', fd
return kind.mime, kind.extension, fd
- else:
- Logger.log('Either not a file or file does not exist.', LogLevel.WARNING, self.log)
+ except Exception as e:
+ Logger.log(repr(e), LogLevel.WARNING, self.log)
return 'invalid', 'invalid', fd
