From fada31bb782b9debd87d85387dfce0e5c8c8ec02 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Wed, 30 Apr 2014 23:23:15 -0700 Subject: [PATCH] fix issues in image from URL code --- src/Posting/QR.coffee | 61 ++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 39 deletions(-) diff --git a/src/Posting/QR.coffee b/src/Posting/QR.coffee index e0c4afffd..adf60a319 100644 --- a/src/Posting/QR.coffee +++ b/src/Posting/QR.coffee @@ -282,57 +282,43 @@ QR = QR.handleFiles files $.addClass QR.nodes.el, 'dump' - handleBlob: (urlBlob, header, url) -> - name = url.substr(url.lastIndexOf('/')+1, url.length) - #QUALITY coding at work - start = header.indexOf("Content-Type: ") + 14 - endsc = header.substr(start, header.length).indexOf(";") - endnl = header.substr(start, header.length).indexOf("\n") - 1 - end = endnl - if (endsc != -1 and endsc < endnl) - end = endsc - mime = header.substr(start, end) + handleBlob: (urlBlob, contentType, contentDisposition, url) -> + name = url.match(/([^\/]+)\/*$/)?[1] + mime = contentType?.match(/[^;]*/)[0] or 'application/octet-stream' + match = + contentDisposition?.match(/\bfilename\s*=\s*"((\\"|[^"])+)"/i)?[1] or + contentType?.match(/\bname\s*=\s*"((\\"|[^"])+)"/i)?[1] + if match + name = match.replace /\\"/g, '"' blob = new Blob([urlBlob], {type: mime}) - blob.name = url.substr(url.lastIndexOf('/')+1, url.length) - 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." + blob.name = name QR.handleFiles([blob]) handleUrl: -> url = prompt("Insert an url:") return if url is null + <% if (type === 'crx') { %> xhr = new XMLHttpRequest(); xhr.open('GET', url, true) xhr.responseType = 'blob' xhr.onload = (e) -> if @readyState is @DONE && xhr.status is 200 - QR.handleBlob(@response, @getResponseHeader('Content-Type'), url) - return + contentType = @getResponseHeader('Content-Type') + contentDisposition = @getResponseHeader('Content-Disposition') + QR.handleBlob @response, contentType, contentDisposition, url else QR.error "Can't load image." - return - xhr.onerror = (e) -> QR.error "Can't load image." - return - xhr.send() - return <% } %> <% if (type === 'userscript') { %> - GM_xmlhttpRequest { - method: "GET", - url: url, - overrideMimeType: "text/plain; charset=x-user-defined", + GM_xmlhttpRequest + method: "GET" + url: url + overrideMimeType: "text/plain; charset=x-user-defined" onload: (xhr) -> r = xhr.responseText data = new Uint8Array(r.length) @@ -340,14 +326,11 @@ QR = while i < r.length data[i] = r.charCodeAt(i) i++ - - QR.handleBlob(data, xhr.responseHeaders, url) - return - - onerror: (xhr) -> - QR.error "Can't load image." - } - return + contentType = xhr.responseHeaders.match(/Content-Type:\s*(.*)/i)?[1] + contentDisposition = xhr.responseHeaders.match(/Content-Disposition:\s*(.*)/i)?[1] + QR.handleBlob data, contentType, contentDisposition, url + onerror: (xhr) -> + QR.error "Can't load image." <% } %> handleFiles: (files) ->