Skip to content
Snippets Groups Projects
Commit 9cd4bda9 authored by Maki Nishifuji's avatar Maki Nishifuji
Browse files

detect charset and convert to utf-8

parent 43daeb1a
No related branches found
No related tags found
No related merge requests found
node_modules
This directory and the files immediately inside it are automatically generated
when you change this package's NPM dependencies. Commit the files in this
directory (npm-shrinkwrap.json, .gitignore, and this README) to source control
so that others run the same versions of sub-dependencies.
You should NOT check in the node_modules directory that Meteor automatically
creates; if you are using git, the .gitignore file tells git to ignore it.
{
"dependencies": {
"iconv-lite": {
"version": "0.4.13"
}
}
}
......@@ -5,6 +5,10 @@ Package.describe({
git: ''
});
Npm.depends({
'iconv-lite': '0.4.13'
});
Package.onUse(function(api) {
api.versionsFrom('1.0');
......
......@@ -3,12 +3,24 @@ http = Npm.require('http')
https = Npm.require('https')
zlib = Npm.require('zlib')
querystring = Npm.require('querystring')
iconv = Npm.require('iconv-lite')
gunzipSync = Meteor.wrapAsync zlib.gunzip.bind(zlib)
inflateSync = Meteor.wrapAsync zlib.inflate.bind(zlib)
OEmbed = {}
# Detect encoding
getCharset = (body) ->
binary = body.toString 'binary'
matches = binary.match /<meta\b[^>]*charset=["']?([\w\-]+)/i
if matches
return matches[1]
return 'utf-8'
toUtf8 = (body) ->
return iconv.decode body, getCharset(body)
getUrlContent = (urlObj, redirectCount = 5, callback) ->
if _.isString(urlObj)
urlObj = URL.parse urlObj
......@@ -63,7 +75,7 @@ getUrlContent = (urlObj, redirectCount = 5, callback) ->
callback null, {
headers: response.headers
body: buffer.toString()
body: toUtf8 buffer
parsedUrl: parsedUrl
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment