better handling of 404. Still not perfect

This commit is contained in:
Mathieu PATUREL
2017-01-02 18:03:24 +11:00
parent 6a0267fb3b
commit 93a04733da
5 changed files with 42 additions and 19 deletions

View File

@ -2,9 +2,8 @@ Fast:
☐ sync scroll @needsUpdate(because of images) ☐ sync scroll @needsUpdate(because of images)
☐ regive focus to the right markdown view ☐ regive focus to the right markdown view
bug when empty `src` call settings listener on_new too - might be too heavy
call settings listener on_new too use alt attribute for 404 error
☐ try/except for 404
Medium: Medium:
☐ auto refresh preview if loading images ☐ auto refresh preview if loading images
@ -15,13 +14,17 @@ Long:
☐ fix #4 ☐ fix #4
☐ support hanchor (TOC) @big ☐ support hanchor (TOC) @big
Unknown:
☐ check how many times is the show_html function called
___________________ ___________________
Archive: Archive:
preview.set_scratch(True) @done (17-01-02 16:41) @project(Fast) try/except for 404 @done Mon 02 Jan 2017 at 18:03 @project(Fast)
set the title of the preview @done (17-01-02 16:38) @project(Fast) fix bug when empty `src` @done Mon 02 Jan 2017 at 17:15 @project(Fast)
✔ preview.wordWrap => True @done (17-01-02 16:31) @project(Fast) ✔ preview.set_scratch(True) @done Mon 02 Jan 2017 at 16:58
clean the code (syntax) @done (17-01-02 16:27) @project(Medium) set the title of the preview @done Mon 02 Jan 2017 at 16:58
add 404 image @done (17-01-02 16:27) @project(Medium) preview.wordWrap => True @done Mon 02 Jan 2017 at 16:58
load images from internet (`https:`) @done (17-01-02 15:51) @project(todo) clean the code (syntax) @done Mon 02 Jan 2017 at 16:58
✔ add 404 image @done Mon 02 Jan 2017 at 16:57
✔ load images from internet (`https:`) @done Mon 02 Jan 2017 at 16:57

View File

@ -12,7 +12,7 @@ def to_base64(path=None, content=None):
try: try:
with open(path, 'rb') as fp: with open(path, 'rb') as fp:
content = fp.read() content = fp.read()
except FileNotFoundError: except (FileNotFoundError, OSError):
return to_base64(file404) return to_base64(file404)
return 'data:image/png;base64,' + ''.join([chr(el) for el in list(base64.standard_b64encode(content))]) return 'data:image/png;base64,' + ''.join([chr(el) for el in list(base64.standard_b64encode(content))])
@ -34,9 +34,9 @@ def mini(val, min):
def get_content_till(string, char_to_look_for, start=0): def get_content_till(string, char_to_look_for, start=0):
i = start i = start
while i < len(string): while i < len(string):
i += 1
if string[i] == char_to_look_for: if string[i] == char_to_look_for:
return string[start:i], i return string[start:i], i
i += 1
def get_view_content(view): def get_view_content(view):
return view.substr(sublime.Region(0, view.size())) return view.substr(sublime.Region(0, view.size()))

View File

@ -2,7 +2,7 @@
import os.path import os.path
from threading import Thread from threading import Thread
import urllib.request import urllib.request, urllib.error
import sublime import sublime
from .functions import * from .functions import *
@ -13,7 +13,19 @@ SEPARATOR = '---%cache%--'
def get_base64_saver(loading, url): def get_base64_saver(loading, url):
def callback(content): def callback(content):
if isinstance(content, urllib.error.HTTPError):
if content.getcode() == 404:
loading[url] = 404
return
elif isinstance(content, urllib.error.URLError):
if (content.reason.errno == 11001 and
content.reason.strerror == 'getaddrinfo failed'):
loading[url] = 404
return
return sublime.error_message('An unexpected error has occured: ' +
str(content))
loading[url] = to_base64(content=content) loading[url] = to_base64(content=content)
return callback return callback
def get_cache_for(imageurl): def get_cache_for(imageurl):
@ -37,8 +49,11 @@ class ImageLoader(Thread):
self.callback = callback self.callback = callback
def run(self): def run(self):
try:
page = urllib.request.urlopen(self.url, None, TIMEOUT) page = urllib.request.urlopen(self.url, None, TIMEOUT)
# self.callback(self.url, page.read()) except Exception as e:
self.callback(e)
else:
self.callback(page.read()) self.callback(page.read())
@ -53,14 +68,11 @@ class ImageManager(object):
# run an other request # run an other request
>>> image = ImageManager.get('http://domain.com/image.png') >>> image = ImageManager.get('http://domain.com/image.png')
'data:image/png;base64,....' 'data:image/png;base64,....'
""" """
loading = {} loading = {}
@staticmethod @staticmethod
def get(imageurl, user_callback=None): def get(imageurl, user_callback=None):
# if imageurl in ImageManager.loading.keys():
# return None
cached = get_cache_for(imageurl) cached = get_cache_for(imageurl)
if cached: if cached:
@ -69,6 +81,8 @@ class ImageManager(object):
# return None (the file is still loading, already made a request) # return None (the file is still loading, already made a request)
# return string the base64 of the url (which is going to be cached) # return string the base64 of the url (which is going to be cached)
temp_cached = ImageManager.loading[imageurl] temp_cached = ImageManager.loading[imageurl]
if temp_cached == 404:
return to_base64('404.png')
if temp_cached: if temp_cached:
cache(imageurl, temp_cached) cache(imageurl, temp_cached)
del ImageManager.loading[imageurl] del ImageManager.loading[imageurl]

View File

@ -138,7 +138,7 @@ def create_preview(window, md_view):
return preview return preview
def show_html(md_view, preview): def show_html(md_view, preview):
html = '<style>{}</style>{}'.format(get_style(), html = '<style>{}</style>\n{}'.format(get_style(),
pre_with_br(markdown2.markdown(get_view_content(md_view), pre_with_br(markdown2.markdown(get_view_content(md_view),
extras=['fenced-code-blocks', extras=['fenced-code-blocks',
'no-code-highlighting']))) 'no-code-highlighting'])))

View File

@ -1,3 +1,9 @@
# DuckDuckGo - The Search engine you'll fall in love with # DuckDuckGo - The Search engine you'll fall in love with
![DuckDUckGo](http://www.articpost.com/wp-content/uploads/2014/08/DuckDuckGo-Search-Engine.jpg) ![DuckDUckGo](http://www.articpost.com/wp-content/uploads/2014/08/DuckDuckGo-Search-Engine.jpg)
![image](http://afterishtar.pl/images/100x100.gif)
![image](http://erewer.omc/dsfdsf)
This is a test