From f9a05332e51b586b42cffcf144dae6f4b3abe436 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 12 Jun 2014 18:01:20 -0400 Subject: client side of uploading process --- public/assets/javascripts/ui/lib/Parser.js | 96 ++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 public/assets/javascripts/ui/lib/Parser.js (limited to 'public/assets/javascripts/ui/lib/Parser.js') diff --git a/public/assets/javascripts/ui/lib/Parser.js b/public/assets/javascripts/ui/lib/Parser.js new file mode 100644 index 0000000..545ed41 --- /dev/null +++ b/public/assets/javascripts/ui/lib/Parser.js @@ -0,0 +1,96 @@ +var Parser = { + integrations: [{ + type: 'image', + regex: /\.(jpeg|jpg|gif|png|svg)(\?.*)?$/i, + async: false, + fetch: function(url, done) { + done("", "") + }, + tag: function (media) { + return ''; + } + }, { + type: 'youtube', + regex: /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i, + async: false, + fetch: function(url, done) { + var id = (url.match(/v=([-_a-zA-Z0-9]{11})/i) || url.match(/youtu.be\/([-_a-zA-Z0-9]{11})/i) || url.match(/embed\/([-_a-zA-Z0-9]{11})/i))[1].split('&')[0]; + var thumb = "http://i.ytimg.com/vi/" + id + "/hqdefault.jpg" + done(id, thumb); + }, + tag: function (media) { + return ''; + } + }, { + type: 'vimeo', + regex: /vimeo.com\/\d+$/i, + async: true, + fetch: function(url, done) { + var id = url.match(/\d+$/i)[0]; + $.ajax({ + type: 'GET', + url: 'http://vimeo.com/api/v2/video/' + id + '.json', + success: function(result){ + if (result.length == 0) { return done(id, "") } + done(id, result[0].thumbnail_large) + } + }) + }, + tag: function (media) { + return ''; + } + }, + /* + { + type: 'soundcloud', + regex: /soundcloud.com\/[-a-zA-Z0-9]+\/[-a-zA-Z0-9]+\/?$/i, + async: true, + fetch: function (url, done) { + $.ajax({ + type: 'GET', + url: 'http://api.soundcloud.com/resolve.json?url=' + + url + + '&client_id=' + + '0673fbe6fc794a7750f680747e863b10', + success: function(result) { + done(result.id, ""); + } + }); + }, + tag: function (media) { + return '' + } + }, { + type: 'link', + regex: /^http.+/i, + async: false, + fetch: function(url, done) { + done("", "") + }, + tag: function (media) { + return '' + media.url + '' + } + } + */ + ], + + parse: function (url, cb) { + Parser.integrations.some(function(integration){ + if (integration.regex.test(url)) { + integration.fetch(url, function(token, thumbnail){ + cb({ + token: token, + thumbnail: thumbnail, + type: integration.type, + url: url, + }) + }) + return true + } + return false + }) + cb(null) + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2