Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I found a bug from"evernote: insert attachment here" in windows #137

Closed
wants to merge 5 commits into from

Conversation

358463121
Copy link

@358463121 358463121 commented May 6, 2016

when I press the command evernote: insert attachment here,i was asked to
type a filename("C:/123.jpg") .But i got an error message
:

error: Evernote plugin has troubles locating the specified file/URL.
Evernote plugin error, please see the console for more details.
Then contact developer at
https://github.com/bordaigorl/sublime-evernote/issues
Evernote plugin error:

After I search the google, i found there are something wrongs in the
source code(line 1114):

https://github.com/bordaigorl/sublime-evernote/blob/master/sublime_evernote.py#L1113

Related code:

if urllib.parse.urlparse(filename).scheme != "":
# download
response = urllib.request.urlopen(filename)
filecontents = response.read()
attr = {"sourceURL": filename}
mimet = response.info().get_content_type()
else:
datafile = os.path.expanduser(filename)
with open(datafile, 'rb') as content_file:
filecontents = content_file.read()
attr = {"fileName": os.path.basename(datafile)}

where urllib.parse.urlparse(filename).scheme in windows, filename will
be "C:/123.jpg"
and this statement will return a "C" ,so the filename "C:/123.jpg"
will be considered as an url according to this code.

so one of the solution for this bug in windows is that:

if urllib.parse.urlparse(filename).scheme != ""  and
urllib.parse.urlparse(filename).scheme not in [chr(i) for i in
range(97,123)] :
# download
response = urllib.request.urlopen(filename)
filecontents = response.read()
attr = {"sourceURL": filename}
mimet = response.info().get_content_type()
else:
datafile = os.path.expanduser(filename)
with open(datafile, 'rb') as content_file:
filecontents = content_file.read()
attr = {"fileName": os.path.basename(datafile)}

use this statement instead

urllib.parse.urlparse(filename).scheme != ""  and
urllib.parse.urlparse(filename).scheme not in [chr(i) for i in
range(97,123)]

which means that to find out if the 'C' is a letter, so as to separate
the area HTTPS FTP HTTP and orther urls

e.g.

>>> filename='http://www.google.com/123.jpg'
>>> urllib.parse.urlparse(filename).scheme != ""  and
urllib.parse.urlparse(filename).scheme not in [chr(i) for i in
range(97,123)]
True
>>> filename="/123/123.jpg"
>>> urllib.parse.urlparse(filename).scheme != ""  and
urllib.parse.urlparse(filename).scheme not in [chr(i) for i in
range(97,123)]
False
>>> filename='c:/123.jpg'
>>> urllib.parse.urlparse(filename).scheme != ""  and
urllib.parse.urlparse(filename).scheme not in [chr(i) for i in
range(97,123)]
False

when I press the command evernote: insert attachment here,i was asked to
type a filename(what i type is:"C:/123.jpg") .But i got an error message
:

error: Evernote plugin has troubles locating the specified file/URL.
Evernote plugin error, please see the console for more details.
Then contact developer at
https://github.com/bordaigorl/sublime-evernote/issues
Evernote plugin error: <urlopen error unknown url type: g>

After I search the google, i found there are something wrongs in the
source code(line 1114):

https://github.com/bordaigorl/sublime-evernote/blob/master/sublime_evernote.py#L1113

Relate code:

if urllib.parse.urlparse(filename).scheme != "":
# download
response = urllib.request.urlopen(filename)
filecontents = response.read()
attr = {"sourceURL": filename}
mimet = response.info().get_content_type()
else:
datafile = os.path.expanduser(filename)
with open(datafile, 'rb') as content_file:
filecontents = content_file.read()
attr = {"fileName": os.path.basename(datafile)}

where "urllib.parse.urlparse(filename).scheme" in windows, filename will
be "C:/123.jpg"
and this statement will return a "C"  ,so the filename  "C:/123.jpg"
will be considered as an url according to this code.

so one of  the solution for this bug in windows is that:

if urllib.parse.urlparse(filename).scheme != ""  and
urllib.parse.urlparse(filename).scheme not in [chr(i) for i in
range(97,123)] :
# download
response = urllib.request.urlopen(filename)
filecontents = response.read()
attr = {"sourceURL": filename}
mimet = response.info().get_content_type()
else:
datafile = os.path.expanduser(filename)
with open(datafile, 'rb') as content_file:
filecontents = content_file.read()
attr = {"fileName": os.path.basename(datafile)}

use this statement instead

urllib.parse.urlparse('c:/123.jpg').scheme not in [chr(i) for i in
range(97,123)]

which means that to find out if the 'C' is a letter, so as to separate
the area HTTPS FTP HTTP and orther urls

e.g.
>>> filename='http://www.google.com/123.jpg'
>>> urllib.parse.urlparse(filename).scheme != ""  and
urllib.parse.urlparse(filename).scheme not in [chr(i) for i in
range(97,123)]
True
>>> filename="/123/123.jpg"
>>> urllib.parse.urlparse(filename).scheme != ""  and
urllib.parse.urlparse(filename).scheme not in [chr(i) for i in
range(97,123)]
False
>>> filename='c:/123.jpg'
>>> urllib.parse.urlparse(filename).scheme != ""  and
urllib.parse.urlparse(filename).scheme not in [chr(i) for i in
range(97,123)]
False
@bordaigorl
Copy link
Owner

Hi @358463121 thanks for your contribution.

I think the correct patch for this is not by insisting in using urlparse but by checking if the string is a file, i.e. substituting the urlparse check with os.path.isfile(filename).

bordaigorl added a commit that referenced this pull request Jun 12, 2016
Use of urlparse to check if string is url wrongly interpreted
`C:/path` as an url.
@bordaigorl
Copy link
Owner

Hi, thanks again for the contribution.
I am preparing the new release and fixed the bug manually as in your last commit.
Thanks!

@bordaigorl bordaigorl closed this Jun 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants