Browse Source

experimental attachment support using webtorrent.

persist attached file (local added torrents) using indexedDB/localForage lib.
readme-update
Miguel Freitas 9 years ago
parent
commit
ca911842c1
  1. 3
      home.html
  2. 12
      js/interface_common.js
  3. 80
      js/interface_home.js
  4. 7
      js/localforage.min.js

3
home.html

@ -126,6 +126,9 @@
<form class="post-area-new"> <form class="post-area-new">
<textarea placeholder="New Post..."></textarea> <textarea placeholder="New Post..."></textarea>
<div class="post-area-extras"> <div class="post-area-extras">
<div class="post-area-attach" style="display: none;">
<input type="file" id="fileInputAttach" style="font-size: 10px;/>
</div>
<span class="post-area-remaining">140</span> <span class="post-area-remaining">140</span>
<button class="undo-unicode disabled" disabled="disabled">undo</button> <button class="undo-unicode disabled" disabled="disabled">undo</button>
<button class="post-submit disabled" disabled="disabled">post</button> <button class="post-submit disabled" disabled="disabled">post</button>

12
js/interface_common.js

@ -930,8 +930,10 @@ function applyShortenedURI(short, uriAndMimetype) {
if ($.Options.WebTorrent.val === 'enable') { if ($.Options.WebTorrent.val === 'enable') {
if ($.Options.WebTorrentAutoDownload.val === 'enable' && if ($.Options.WebTorrentAutoDownload.val === 'enable' &&
followingUsers.indexOf(fromUser) !==-1) { followingUsers.indexOf(fromUser) !==-1) {
twister.torrentIds[long] = true; if(!(long in twister.torrentIds)) {
$.localStorage.set('torrentIds', twister.torrentIds); twister.torrentIds[long] = true;
$.localStorage.set('torrentIds', twister.torrentIds);
}
startTorrentDownloadAndPreview(long, previewContainer, isMedia); startTorrentDownloadAndPreview(long, previewContainer, isMedia);
} else { } else {
// webtorrent enabled but no auto-download. provide a link to start manually. // webtorrent enabled but no auto-download. provide a link to start manually.
@ -987,9 +989,11 @@ function webtorrentFilePreview(file, previewContainer, isMedia) {
var imagePreview = $('<div class="image-preview" />'); var imagePreview = $('<div class="image-preview" />');
previewContainer.append(imagePreview); previewContainer.append(imagePreview);
file.appendTo(imagePreview[0], function (err, elem) { file.appendTo(imagePreview[0], function (err, elem) {
elem.pause(); if ('pause' in elem) {
elem.pause();
}
}); });
imagePreview.find("video").removeAttr("autoplay").get(0).pause(); imagePreview.find("video").removeAttr("autoplay");
} else { } else {
file.getBlobURL(function (err, url) { file.getBlobURL(function (err, url) {
if (err) return console.error(err) if (err) return console.error(err)

80
js/interface_home.js

@ -310,7 +310,7 @@ function initWebTorrent() {
localStorage.removeItem('debug') localStorage.removeItem('debug')
WEBTORRENT_ANNOUNCE = $.Options.WebTorrentTrackers.val.split(/[ ,]+/) WEBTORRENT_ANNOUNCE = $.Options.WebTorrentTrackers.val.split(/[ ,]+/)
$.getScript('js/webtorrent.min.js', function( data, textStatus, jqxhr ) { $.getScript('js/webtorrent.min.js', function() {
WebTorrentClient = new WebTorrent(); WebTorrentClient = new WebTorrent();
console.log("WebTorrent started") console.log("WebTorrent started")
WebTorrentClient.on('error', function (err) { WebTorrentClient.on('error', function (err) {
@ -322,14 +322,78 @@ function initWebTorrent() {
if ($.localStorage.isSet('torrentIds')) if ($.localStorage.isSet('torrentIds'))
twister.torrentIds = $.localStorage.get('torrentIds'); twister.torrentIds = $.localStorage.get('torrentIds');
if ($.Options.WebTorrentAutoDownload.val === 'enable') {
for (var torrentId in twister.torrentIds) { $.getScript('js/localforage.min.js', function() {
if( twister.torrentIds[torrentId] === true ) { localforage.setDriver([localforage.INDEXEDDB,localforage.WEBSQL]).then(function() {
console.log("WebTorrent auto-download: " + torrentId); for (var torrentId in twister.torrentIds) {
WebTorrentClient.add(torrentId); if( twister.torrentIds[torrentId] ) {
if (typeof(twister.torrentIds[torrentId]) === "string") {
// get blob file to restart seeding this file
var onGetItem = function(torrentId, err, data) {
console.log("onget:", torrentId, err, data)
if (err || data === null) {
// error reading blob, just add torrentId
console.log("WebTorrent auto-download: " + torrentId +
" (previously seeded as: " + twister.torrentIds[torrentId] + ")" );
WebTorrentClient.add(torrentId);
} else {
var fileBlob = new File([data], twister.torrentIds[torrentId]);
console.log('WebTorrent seeding: "' + twister.torrentIds[torrentId] +
'" size: ' + data.size);
WebTorrentClient.seed(fileBlob);
}
}
localforage.getItem(torrentId, onGetItem.bind(null, torrentId));
} else if ($.Options.WebTorrentAutoDownload.val === 'enable') {
console.log("WebTorrent auto-download: " + torrentId);
WebTorrentClient.add(torrentId);
}
}
} }
}
} // setup attach button
$(".post-area-attach").show();
var fileInput = $("#fileInputAttach");
fileInput.on('change', function(event) {
var file = fileInput[0].files[0];
var seedingTorrent = undefined;
for (var i = 0; i < WebTorrentClient.torrents.length; i++) {
var torrent = WebTorrentClient.torrents[i];
if (torrent.length === file.size &&
torrent.files[0].name === file.name) {
seedingTorrent = torrent;
}
}
var saveBlobFile = function(infoHash,file) {
var magnetLink = "magnet:?xt=urn:btih:" + infoHash
var blobFile = new Blob([file]);
localforage.setItem(magnetLink, blobFile);
twister.torrentIds[magnetLink] = file.name;
$.localStorage.set('torrentIds', twister.torrentIds);
return magnetLink;
}
if (seedingTorrent) {
var magnetLink = saveBlobFile(seedingTorrent.infoHash,file);
console.log('Already seeding ' + magnetLink);
shortenMagnetLink(event,magnetLink);
} else {
WebTorrentClient.seed(file, function (torrent) {
var magnetLink = saveBlobFile(torrent.infoHash,file);
console.log('Client is seeding ' + magnetLink);
shortenMagnetLink(event,magnetLink);
});
}
});
});
});
});
}
function shortenMagnetLink(event,magnetLink) {
var uri = prompt(polyglot.t('shorten_URI_enter_link'), magnetLink);
var textArea = $(event.target).closest('form').find('textarea');
newShortURI(uri, function(long,short) {
textArea.append(short);
}); });
} }

7
js/localforage.min.js vendored

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save