Commit 92d7f2c7 authored by Xiaowu Zhang's avatar Xiaowu Zhang

remove file cut in indexeddb

parent ae076f34
......@@ -48,34 +48,6 @@
gadget.analyser.connect(gadget.gain);
gadget.gain.connect(audioCtx.destination);
}
function loopEvent(g) {
return new RSVP.Queue().push(function() {
if (g.rebuild) {
return g.jio_getAttachment({
_id: g.id,
_attachment: "enclosure" + g.index
});
}
}).push(function(blob) {
if (g.rebuild) {
g.index += 1;
if (g.blob) {
g.blob = new Blob([ g.blob, blob ], {
type: "audio/mpeg"
});
} else {
g.blob = new Blob([ blob ], {
type: "audio/mpeg"
});
}
if (g.index < g.length) {
return loopEvent(g);
}
}
}).push(undefined, function(error) {
throw error;
});
}
function timeFormat(seconds) {
var result = "00:" + Math.round(seconds), min, sec;
if (seconds > 59) {
......@@ -178,7 +150,6 @@
});
}).push(function(url) {
g.__element.getElementsByClassName("next")[0].href = url;
g.index = 0;
g.id = options.id;
return g.jio_get({
_id: options.id
......@@ -186,12 +157,15 @@
}).push(function(result) {
var share_context = g.__element.getElementsByClassName("share")[0];
share_context.href = "https://twitter.com/intent/tweet?hashtags=MusicPlayer&text=" + encodeURI(result.data.title);
g.length = Object.keys(result.data._attachment).length;
g.size = result.data.size;
return g.displayThisTitle(options.action + " : " + result.data.title);
}).push(function() {
g.index = 1e6;
return g.jio_getAttachment({
_id: options.id,
_attachment: "enclosure0"
_attachment: "enclosure",
_start: 0,
_end: 1e6
});
}).push(function(blob) {
g.sourceBuffer = g.mediaSource.addSourceBuffer("audio/mpeg;");
......@@ -230,10 +204,13 @@
$(time_context).offset().top = $(bar_context).offset().top + 3;
time_context.innerHTML = timeFormat(g.audio.duration);
if (g.rebuild) {
return loopEvent(g);
return g.jio_getAttachment({
_id: g.id,
_attachment: "enclosure"
});
}
}).push(function() {
if (g.blob) {
}).push(function(blob) {
if (blob) {
g.audio.src = URL.createObjectURL(g.blob);
g.audio.load();
g.audio.play();
......@@ -243,16 +220,18 @@
return;
}
g.fin = false;
if (g.index >= g.length - 1) {
if (g.index >= g.size) {
g.mediaSource.endOfStream();
bar_context.max = g.audio.duration;
return;
}
g.index += 1;
return g.jio_getAttachment({
_id: g.id,
_attachment: "enclosure" + g.index
_attachment: "enclosure",
_start: g.index,
_end: g.index + 1e6
}).then(function(blob) {
g.index += 1e6;
return jIO.util.readBlobAsArrayBuffer(blob);
}).then(function(e) {
g.fin = true;
......
......@@ -20,34 +20,6 @@
}).declareMethod("startService", function() {
var g = this, input_context = g.__element.getElementsByTagName("input")[0], info_context = g.__element.getElementsByClassName("info")[0], queue, uploaded = 0, post, length;
info_context.innerHTML = "<ul>";
function putAll(id, index, file) {
var blobLength = 4e6, size = blobLength * (index + 1), blob;
if (size > file.size) {
blob = file.slice(blobLength * index, file.size);
} else {
blob = file.slice(blobLength * index, size);
}
return g.jio_putAttachment({
_id: id,
_attachment: "enclosure" + index,
_blob: blob
}, 0).then(function() {
var progress = Math.floor(size / file.size * 100);
if (progress > 100) {
progress = 100;
}
progress += "%";
info_context.innerHTML += "<li>" + file.name + " " + progress + "</li>";
if (size < file.size) {
return putAll(id, index + 1, file);
}
uploaded += 1;
if (uploaded === length) {
return exit(g);
}
queue.push(post);
});
}
post = function() {
var now = new Date(), id;
if (uploaded === length) {
......@@ -61,10 +33,19 @@
modified: now.toUTCString(),
date: now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate()
}, 0).then(function(res) {
var tmp = uploaded + 1;
id = res.id;
info_context.innerHTML += "<li>" + input_context.files[uploaded].name + " " + tmp + "/" + length;
return putAll(id, 0, input_context.files[uploaded]);
return g.jio_putAttachment({
_id: res.id,
_attachment: "enclosure",
_blob: input_context.files[uploaded]
}, 0);
}).then(function() {
uploaded += 1;
info_context.innerHTML += "<li>" + input_context.files[uploaded - 1].name + " " + uploaded + "/" + length + "</li>";
if (uploaded === length) {
return exit(g);
}
queue.push(post);
}).fail(function(error) {
if (!(error instanceof RSVP.CancellationError)) {
info_context.innerHTML += input_context.files[uploaded].name + " " + error.target.error.name;
......
......@@ -10,30 +10,6 @@
(function(window, rJS, RSVP, loopEventListener, $, promiseEventListener) {
"use strict";
var gk = rJS(window), MediaSource = window.MediaSource || window.WebKitMediaSource || window.mozMediaSource;
function loopEvent(g) {
return new RSVP.Queue().push(function() {
if (g.rebuild) {
return g.jio_getAttachment({
_id: g.id,
_attachment: "enclosure" + g.index
});
}
}).push(function(blob) {
if (g.rebuild) {
g.index += 1;
if (g.blob) {
g.blob = new Blob([ g.blob, blob ]);
} else {
g.blob = new Blob([ blob ]);
}
if (g.index < g.length) {
return loopEvent(g);
}
}
}).push(undefined, function(error) {
throw error;
});
}
gk.declareAcquiredMethod("jio_getAttachment", "jio_getAttachment").declareAcquiredMethod("jio_get", "jio_get").declareAcquiredMethod("jio_remove", "jio_remove").declareAcquiredMethod("plSave", "plSave").declareAcquiredMethod("plGive", "plGive").declareAcquiredMethod("displayThisPage", "displayThisPage").declareAcquiredMethod("displayThisTitle", "displayThisTitle").declareAcquiredMethod("allDocs", "allDocs").declareAcquiredMethod("plEnablePage", "plEnablePage").declareAcquiredMethod("pleaseRedirectMyHash", "pleaseRedirectMyHash").declareMethod("render", function(options) {
var g = this;
if (options.id) {
......@@ -45,7 +21,7 @@
}).push(function(result) {
var share_context = g.__element.getElementsByClassName("share")[0];
share_context.href = "https://twitter.com/intent/tweet?hashtags=MusicPlayer&text=" + encodeURI(result.data.title);
g.length = Object.keys(result.data._attachment).length;
g.size = result.data.size;
return g.displayThisTitle(options.action + " : " + result.data.title);
}).push(function() {
return g.allDocs({
......@@ -71,11 +47,13 @@
});
}).push(function(url) {
g.__element.getElementsByClassName("next")[0].href = url;
g.index = 0;
g.id = options.id;
g.index = 35e5;
return g.jio_getAttachment({
_id: options.id,
_attachment: "enclosure0"
_attachment: "enclosure",
_start: 0,
_end: 35e5
});
}).push(function(blob) {
g.sourceBuffer = g.mediaSource.addSourceBuffer('video/webm; codecs="vorbis,vp8"');
......@@ -99,10 +77,13 @@
return g.plEnablePage();
}).push(function() {
if (g.rebuild) {
return loopEvent(g);
return g.jio_getAttachment({
_id: g.id,
_attachment: "enclosure"
});
}
}).push(function() {
if (g.blob) {
}).push(function(blob) {
if (blob) {
g.video.src = URL.createObjectURL(g.blob);
g.video.load();
g.video.play();
......@@ -112,15 +93,17 @@
return;
}
g.fin = false;
if (g.index >= g.length - 1) {
if (g.index >= g.size) {
g.mediaSource.endOfStream();
return;
}
g.index += 1;
return g.jio_getAttachment({
_id: g.id,
_attachment: "enclosure" + g.index
_attachment: "enclosure",
_start: g.index,
_end: g.index + 35e5
}).then(function(blob) {
g.index += 35e5;
return jIO.util.readBlobAsArrayBuffer(blob);
}).then(function(e) {
g.fin = true;
......
......@@ -9157,7 +9157,7 @@ function sequence(thens) {
*/
/*jslint indent: 2, maxlen: 80, nomen: true */
/*global define, module, require, indexedDB, jIO, RSVP, Blob*/
/*global define, module, require, indexedDB, jIO, RSVP, Blob, Math*/
(function (dependencies, factory) {
"use strict";
......@@ -9841,7 +9841,7 @@ function sequence(thens) {
blob,
totalLength;
function getDesirePart(store, start, end) {
if (start >= end) {
if (start > end) {
return;
}
return getIndexedDB(store, [param._id, param._attachment, start])
......@@ -9870,27 +9870,37 @@ function sequence(thens) {
param._id, "attachment", "_id");
})
.push(function (researchResult) {
var result = researchResult.result;
var result = researchResult.result,
start,
end;
if (result === undefined ||
result._attachment[param._attachment] === undefined) {
throw ({"status": 404, "reason": "missing attachment",
"message": "IndexeddbStorage, unable to get attachment."});
}
totalLength = result._attachment[param._attachment].length;
param._seek = param._seek === undefined ? 0 : param._seek;
param._offset = param._offset === undefined ? totalLength
: param._offset;
if (param._seek > param._offset) {
param._start = param._start === undefined ? 0 : param._start;
param._end = param._end === undefined ? totalLength
: param._end;
if (param._end > totalLength) {
param._end = totalLength;
}
if (param._start > param._end) {
throw ({"status": 404, "reason": "invalide offset",
"message": "seek is great then offset"});
"message": "start is great then end"});
}
start = Math.floor(param._start / jio_storage._unite);
end = Math.floor(param._end / jio_storage._unite);
if (param._end % jio_storage._unite === 0) {
end -= 1;
}
return getDesirePart(transaction.objectStore("blob"),
param._seek / jio_storage._unite,
param._offset / jio_storage._unite);
start,
end);
})
.push(function () {
var start = param._seek % jio_storage._unite,
end = start + param._offset - param._seek;
var start = param._start % jio_storage._unite,
end = start + param._end - param._start;
blob = blob.slice(start, end);
return ({ "data": new Blob([blob], {type: "text/plain"})});
})
......
......@@ -63,31 +63,6 @@
}
function loopEvent(g) {
return new RSVP.Queue()
.push(function () {
if (g.rebuild) {
return g.jio_getAttachment({"_id" : g.id,
"_attachment" : "enclosure" + g.index });
}
})
.push(function (blob) {
if (g.rebuild) {
g.index += 1;
if (g.blob) {
g.blob = new Blob([g.blob, blob], {type: "audio/mpeg"});
} else {
g.blob = new Blob([blob], {type: "audio/mpeg"});
}
if (g.index < g.length) {
return loopEvent(g);
}
}
})
.push(undefined, function (error) {
throw error;
});
}
function timeFormat(seconds) {
var result = '00:' + Math.round(seconds),
min,
......@@ -235,7 +210,6 @@
})
.push(function (url) {
g.__element.getElementsByClassName("next")[0].href = url;
g.index = 0;
g.id = options.id;
return g.jio_get({"_id" : options.id});
})
......@@ -244,13 +218,16 @@
share_context.href =
"https://twitter.com/intent/tweet?hashtags=MusicPlayer&text="
+ encodeURI(result.data.title);
g.length = Object.keys(result.data._attachment).length;
g.size = result.data.size;
return g.displayThisTitle(options.action + " : "
+ result.data.title);
})
.push(function () {
g.index = 1000000;
return g.jio_getAttachment({"_id" : options.id,
"_attachment" : "enclosure0" });
"_attachment" : "enclosure",
"_start": 0,
"_end": 1000000});
})
.push(function (blob) {
g.sourceBuffer = g.mediaSource.addSourceBuffer('audio/mpeg;');
......@@ -309,11 +286,12 @@
$(time_context).offset().top = $(bar_context).offset().top + 3;
time_context.innerHTML = timeFormat(g.audio.duration);
if (g.rebuild) {
return loopEvent(g);
return g.jio_getAttachment({"_id" : g.id,
"_attachment" : "enclosure"});
}
})
.push(function () {
if (g.blob) {
.push(function (blob) {
if (blob) {
g.audio.src = URL.createObjectURL(g.blob);
g.audio.load();
g.audio.play();
......@@ -325,15 +303,17 @@
return;
}
g.fin = false;
if (g.index >= g.length - 1) {
if (g.index >= g.size) {
g.mediaSource.endOfStream();
bar_context.max = g.audio.duration;
return;
}
g.index += 1;
return g.jio_getAttachment({"_id" : g.id,
"_attachment" : "enclosure" + g.index })
"_attachment" : "enclosure",
"_start": g.index,
"_end": g.index + 1000000})
.then(function (blob) {
g.index += 1000000;
return jIO.util.readBlobAsArrayBuffer(blob);
})
.then(function (e) {
......
......@@ -39,38 +39,6 @@
post,
length;
info_context.innerHTML = "<ul>";
function putAll(id, index, file) {
var blobLength = 4000000,
size = blobLength * (index + 1),
blob;
if (size > file.size) {
blob = file.slice(blobLength * index, file.size);
} else {
blob = file.slice(blobLength * index, size);
}
return g.jio_putAttachment({
"_id" : id,
"_attachment" : "enclosure" + index,
"_blob": blob
}, 0)
.then(function () {
var progress = Math.floor((size / file.size) * 100);
if (progress > 100) {
progress = 100;
}
progress += "%";
info_context.innerHTML += "<li>" + file.name
+ " " + progress + "</li>";
if (size < file.size) {
return putAll(id, index + 1, file);
}
uploaded += 1;
if (uploaded === length) {
return exit(g);
}
queue.push(post);
});
}
post = function () {
var now = new Date(),
id;
......@@ -86,18 +54,28 @@
(now.getMonth() + 1) + "-" + now.getDate()
}, 0)
.then(function (res) {
var tmp = uploaded + 1;
id = res.id;
return g.jio_putAttachment({
"_id" : res.id,
"_attachment" : "enclosure",
"_blob": input_context.files[uploaded]
}, 0);
})
.then(function () {
uploaded += 1;
info_context.innerHTML += "<li>" +
input_context.files[uploaded].name + " " +
tmp + "/" + length;
return putAll(id, 0, input_context.files[uploaded]);
input_context.files[uploaded - 1].name
+ " " + uploaded + "/" + length + "</li>";
if (uploaded === length) {
return exit(g);
}
queue.push(post);
})
.fail(function (error) {
if (!(error instanceof RSVP.CancellationError)) {
info_context.innerHTML +=
input_context.files[uploaded].name + " " +
error.target.error.name;
error.target.error.name;
//xxx
g.plEnablePage();
return g.jio_remove({"_id" : id});
......@@ -107,9 +85,6 @@
});
};
queue = new RSVP.Queue();
queue.push(function () {
return g.plEnablePage();
......
......@@ -17,31 +17,6 @@
var gk = rJS(window),
MediaSource = window.MediaSource || window.WebKitMediaSource
|| window.mozMediaSource;
function loopEvent(g) {
return new RSVP.Queue()
.push(function () {
if (g.rebuild) {
return g.jio_getAttachment({"_id" : g.id,
"_attachment" : "enclosure" + g.index });
}
})
.push(function (blob) {
if (g.rebuild) {
g.index += 1;
if (g.blob) {
g.blob = new Blob([g.blob, blob]);
} else {
g.blob = new Blob([blob]);
}
if (g.index < g.length) {
return loopEvent(g);
}
}
})
.push(undefined, function (error) {
throw error;
});
}
gk.declareAcquiredMethod("jio_getAttachment", "jio_getAttachment")
.declareAcquiredMethod("jio_get", "jio_get")
.declareAcquiredMethod("jio_remove", "jio_remove")
......@@ -65,7 +40,7 @@
share_context.href =
"https://twitter.com/intent/tweet?hashtags=MusicPlayer&text="
+ encodeURI(result.data.title);
g.length = Object.keys(result.data._attachment).length;
g.size = result.data.size;
return g.displayThisTitle(options.action + " : "
+ result.data.title);
})
......@@ -95,10 +70,12 @@
})
.push(function (url) {
g.__element.getElementsByClassName("next")[0].href = url;
g.index = 0;
g.id = options.id;
g.index = 3500000;
return g.jio_getAttachment({"_id" : options.id,
"_attachment" : "enclosure0" });
"_attachment" : "enclosure",
"_start": 0,
"_end": 3500000});
})
.push(function (blob) {
g.sourceBuffer = g.mediaSource.addSourceBuffer('video/webm; codecs="vorbis,vp8"');
......@@ -134,11 +111,12 @@
})
.push(function () {
if (g.rebuild) {
return loopEvent(g);
return g.jio_getAttachment({"_id" : g.id,
"_attachment" : "enclosure"});
}
})
.push(function () {
if (g.blob) {
.push(function (blob) {
if (blob) {
g.video.src = URL.createObjectURL(g.blob);
g.video.load();
g.video.play();
......@@ -149,14 +127,16 @@
return;
}
g.fin = false;
if (g.index >= g.length - 1) {
if (g.index >= g.size) {
g.mediaSource.endOfStream();
return;
}
g.index += 1;
return g.jio_getAttachment({"_id" : g.id,
"_attachment" : "enclosure" + g.index })
"_attachment" : "enclosure",
"_start": g.index,
"_end": g.index + 3500000})
.then(function (blob) {
g.index += 3500000;
return jIO.util.readBlobAsArrayBuffer(blob);
})
.then(function (e) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment