fix issues in image from URL code

This commit is contained in:
ccd0 2014-04-30 23:23:15 -07:00
parent 4cb318120b
commit fada31bb78

View File

@ -282,57 +282,43 @@ QR =
QR.handleFiles files QR.handleFiles files
$.addClass QR.nodes.el, 'dump' $.addClass QR.nodes.el, 'dump'
handleBlob: (urlBlob, header, url) -> handleBlob: (urlBlob, contentType, contentDisposition, url) ->
name = url.substr(url.lastIndexOf('/')+1, url.length) name = url.match(/([^\/]+)\/*$/)?[1]
#QUALITY coding at work mime = contentType?.match(/[^;]*/)[0] or 'application/octet-stream'
start = header.indexOf("Content-Type: ") + 14 match =
endsc = header.substr(start, header.length).indexOf(";") contentDisposition?.match(/\bfilename\s*=\s*"((\\"|[^"])+)"/i)?[1] or
endnl = header.substr(start, header.length).indexOf("\n") - 1 contentType?.match(/\bname\s*=\s*"((\\"|[^"])+)"/i)?[1]
end = endnl if match
if (endsc != -1 and endsc < endnl) name = match.replace /\\"/g, '"'
end = endsc
mime = header.substr(start, end)
blob = new Blob([urlBlob], {type: mime}) blob = new Blob([urlBlob], {type: mime})
blob.name = url.substr(url.lastIndexOf('/')+1, url.length) blob.name = name
name_start = header.indexOf('name="') + 6
if (name_start - 6 != -1)
name_end = header.substr(name_start, header.length).indexOf('"')
blob.name = header.substr(name_start, name_end)
return if blob.type is null
QR.error "Unsupported file type."
return unless blob.type in QR.mimeTypes
QR.error "Unsupported file type."
QR.handleFiles([blob]) QR.handleFiles([blob])
handleUrl: -> handleUrl: ->
url = prompt("Insert an url:") url = prompt("Insert an url:")
return if url is null return if url is null
<% if (type === 'crx') { %> <% if (type === 'crx') { %>
xhr = new XMLHttpRequest(); xhr = new XMLHttpRequest();
xhr.open('GET', url, true) xhr.open('GET', url, true)
xhr.responseType = 'blob' xhr.responseType = 'blob'
xhr.onload = (e) -> xhr.onload = (e) ->
if @readyState is @DONE && xhr.status is 200 if @readyState is @DONE && xhr.status is 200
QR.handleBlob(@response, @getResponseHeader('Content-Type'), url) contentType = @getResponseHeader('Content-Type')
return contentDisposition = @getResponseHeader('Content-Disposition')
QR.handleBlob @response, contentType, contentDisposition, url
else else
QR.error "Can't load image." QR.error "Can't load image."
return
xhr.onerror = (e) -> xhr.onerror = (e) ->
QR.error "Can't load image." QR.error "Can't load image."
return
xhr.send() xhr.send()
return
<% } %> <% } %>
<% if (type === 'userscript') { %> <% if (type === 'userscript') { %>
GM_xmlhttpRequest { GM_xmlhttpRequest
method: "GET", method: "GET"
url: url, url: url
overrideMimeType: "text/plain; charset=x-user-defined", overrideMimeType: "text/plain; charset=x-user-defined"
onload: (xhr) -> onload: (xhr) ->
r = xhr.responseText r = xhr.responseText
data = new Uint8Array(r.length) data = new Uint8Array(r.length)
@ -340,14 +326,11 @@ QR =
while i < r.length while i < r.length
data[i] = r.charCodeAt(i) data[i] = r.charCodeAt(i)
i++ i++
contentType = xhr.responseHeaders.match(/Content-Type:\s*(.*)/i)?[1]
QR.handleBlob(data, xhr.responseHeaders, url) contentDisposition = xhr.responseHeaders.match(/Content-Disposition:\s*(.*)/i)?[1]
return QR.handleBlob data, contentType, contentDisposition, url
onerror: (xhr) -> onerror: (xhr) ->
QR.error "Can't load image." QR.error "Can't load image."
}
return
<% } %> <% } %>
handleFiles: (files) -> handleFiles: (files) ->