mirror of
https://github.com/twisterarmy/twister-html.git
synced 2025-01-26 22:54:45 +00:00
experimental attachment support using webtorrent.
persist attached file (local added torrents) using indexedDB/localForage lib.
This commit is contained in:
parent
d6da998635
commit
ca911842c1
@ -126,6 +126,9 @@
|
||||
<form class="post-area-new">
|
||||
<textarea placeholder="New Post..."></textarea>
|
||||
<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>
|
||||
<button class="undo-unicode disabled" disabled="disabled">undo</button>
|
||||
<button class="post-submit disabled" disabled="disabled">post</button>
|
||||
|
@ -930,8 +930,10 @@ function applyShortenedURI(short, uriAndMimetype) {
|
||||
if ($.Options.WebTorrent.val === 'enable') {
|
||||
if ($.Options.WebTorrentAutoDownload.val === 'enable' &&
|
||||
followingUsers.indexOf(fromUser) !==-1) {
|
||||
twister.torrentIds[long] = true;
|
||||
$.localStorage.set('torrentIds', twister.torrentIds);
|
||||
if(!(long in twister.torrentIds)) {
|
||||
twister.torrentIds[long] = true;
|
||||
$.localStorage.set('torrentIds', twister.torrentIds);
|
||||
}
|
||||
startTorrentDownloadAndPreview(long, previewContainer, isMedia);
|
||||
} else {
|
||||
// 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" />');
|
||||
previewContainer.append(imagePreview);
|
||||
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 {
|
||||
file.getBlobURL(function (err, url) {
|
||||
if (err) return console.error(err)
|
||||
|
@ -310,7 +310,7 @@ function initWebTorrent() {
|
||||
localStorage.removeItem('debug')
|
||||
|
||||
WEBTORRENT_ANNOUNCE = $.Options.WebTorrentTrackers.val.split(/[ ,]+/)
|
||||
$.getScript('js/webtorrent.min.js', function( data, textStatus, jqxhr ) {
|
||||
$.getScript('js/webtorrent.min.js', function() {
|
||||
WebTorrentClient = new WebTorrent();
|
||||
console.log("WebTorrent started")
|
||||
WebTorrentClient.on('error', function (err) {
|
||||
@ -322,14 +322,78 @@ function initWebTorrent() {
|
||||
|
||||
if ($.localStorage.isSet('torrentIds'))
|
||||
twister.torrentIds = $.localStorage.get('torrentIds');
|
||||
if ($.Options.WebTorrentAutoDownload.val === 'enable') {
|
||||
for (var torrentId in twister.torrentIds) {
|
||||
if( twister.torrentIds[torrentId] === true ) {
|
||||
console.log("WebTorrent auto-download: " + torrentId);
|
||||
WebTorrentClient.add(torrentId);
|
||||
|
||||
$.getScript('js/localforage.min.js', function() {
|
||||
localforage.setDriver([localforage.INDEXEDDB,localforage.WEBSQL]).then(function() {
|
||||
for (var torrentId in twister.torrentIds) {
|
||||
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
Normal file
7
js/localforage.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user