Commit bd4038b0 authored by Xiaowu Zhang's avatar Xiaowu Zhang

add download functionality in local mode

parent 3339d9f8
......@@ -115,7 +115,13 @@
return jio_gadget.get.apply(jio_gadget, param_list);
});
}).allowPublicAcquisition("jio_remove", function(param_list) {
return this.getDeclaredGadget(storageType(this.storageType)).push(function(jio_gadget) {
var type;
if (param_list[1] === 0 || param_list[1] === 1) {
type = param_list[1];
} else {
type = this.storageType;
}
return this.getDeclaredGadget(storageType(type)).push(function(jio_gadget) {
return jio_gadget.remove.apply(jio_gadget, param_list);
});
}).allowPublicAcquisition("jio_put", function(param_list) {
......
......@@ -9,6 +9,7 @@
<script src="../lib/handlebars.min.js"></script>
<script src="../mixin_promise/mixin_promise.js" type="text/javascript"></script>
<script src="../lib/id3-minimized.js" type="text/javascript"></script>
<script id="network" type="text/x-handlebars-template">
{{#each rows}}
......@@ -22,6 +23,10 @@
{{this.doc.title}}
</a>
{{/compare}}
<a href=#page=playlist&id={{this.id}}&action=download>
data-rel=popup
data-position-to=window
</a>
</li>
{{/each}}
</script>
......@@ -34,7 +39,7 @@
<input type="search" class="research" placeholder="research..."/>
<div data-role="content">
<ul data-role="listview" data-split-icon="delete" data-split-theme="d">
<ul data-role="listview" data-split-icon="arrow-d" data-split-theme="d">
</ul>
</div>
......
/*global window, rJS, RSVP, console, jQuery, $, JSON, Handlebars,
loopEventListener, RegExp */
loopEventListener, RegExp, ID3, FileAPIReader, Date */
/*jslint maxlen:80, nomen: true */
(function(window, rJS, $, Handlebars, loopEventListener) {
"use strict";
var gk = rJS(window), network_source = gk.__template_element.getElementById("network").innerHTML, network = Handlebars.compile(network_source);
gk.declareAcquiredMethod("allDocs", "allDocs").declareAcquiredMethod("displayThisPage", "displayThisPage").declareAcquiredMethod("displayThisTitle", "displayThisTitle").declareAcquiredMethod("plEnablePage", "plEnablePage").declareAcquiredMethod("pleaseRedirectMyHash", "pleaseRedirectMyHash").declareMethod("render", function(options) {
gk.declareAcquiredMethod("allDocs", "allDocs").declareAcquiredMethod("jio_putAttachment", "jio_putAttachment").declareAcquiredMethod("jio_post", "jio_post").declareAcquiredMethod("jio_remove", "jio_remove").declareAcquiredMethod("jio_getAttachment", "jio_getAttachment").declareAcquiredMethod("displayThisPage", "displayThisPage").declareAcquiredMethod("displayThisTitle", "displayThisTitle").declareAcquiredMethod("plEnablePage", "plEnablePage").declareAcquiredMethod("pleaseRedirectMyHash", "pleaseRedirectMyHash").declareMethod("render", function(options) {
var gadget = this, list = gadget.__element.getElementsByTagName("ul")[0];
return new RSVP.Queue().push(function() {
return RSVP.all([ gadget.displayThisPage({
......@@ -15,8 +15,42 @@
id: "online"
}) ]);
}).push(function(param_list) {
var blob;
gadget.__element.getElementsByClassName("offline")[0].href = param_list[0];
gadget.__element.getElementsByClassName("online")[0].href = param_list[1];
if (options.action === "download") {
return gadget.jio_getAttachment({
_id: options.id,
_attachment: "enclosure"
}).then(function(file) {
var now = new Date(), type;
if (options.id.indexOf(".mp3") === -1) {
type = "video/webm";
} else {
type = "audio/mp3";
}
blob = file;
return gadget.jio_post({
title: options.id,
type: type,
format: type,
size: blob.size,
artist: "unknown",
album: "unknown",
year: "unknown",
picture: "./unknown.jpg",
modified: now.toUTCString(),
date: now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate()
}, 0);
}).then(function(res) {
gadget.putId = res.id;
return gadget.jio_putAttachment({
_id: res.id,
_attachment: "enclosure",
_blob: blob
}, 0);
});
}
}).push(function() {
return gadget.allDocs({
include_docs: true
......@@ -29,7 +63,7 @@
}
return options.inverse(this);
});
if (options.id !== undefined && options.id !== "localhost") {
if (options.id !== undefined && options.id !== "localhost" && options.action !== "download") {
tmp = [];
for (i = 0, j = 0; i < e.data.rows.length; i += 1) {
exp = new RegExp(options.id, "i");
......@@ -47,6 +81,15 @@
return gadget.displayThisTitle("localhost playlist: " + tmp.length + " media");
}).fail(function(error) {
if (!(error instanceof RSVP.CancellationError)) {
if (error.target.error.name === "QuotaExceededError") {
gadget.__element.getElementsByClassName("info")[0].innerHTML = "QuotaError";
if (gadget.putId) {
return gadget.jio_remove({
_id: gadget.putId
}, 0);
}
return;
}
gadget.__element.getElementsByClassName("info")[0].innerHTML = "please enable local server";
}
});
......
......@@ -133,7 +133,13 @@
});
})
.allowPublicAcquisition("jio_remove", function (param_list) {
return this.getDeclaredGadget(storageType(this.storageType))
var type;
if (param_list[1] === 0 || param_list[1] === 1) {
type = param_list[1];
} else {
type = this.storageType;
}
return this.getDeclaredGadget(storageType(type))
.push(function (jio_gadget) {
return jio_gadget.remove.apply(jio_gadget, param_list);
});
......
......@@ -9,6 +9,7 @@
<script src="../<%= copy.handlebars.relative_dest%>"></script>
<script src="../mixin_promise/mixin_promise.js" type="text/javascript"></script>
<script src="../<%= copy.id3.relative_dest %>" type="text/javascript"></script>
<script id="network" type="text/x-handlebars-template">
{{#each rows}}
......@@ -22,6 +23,10 @@
{{this.doc.title}}
</a>
{{/compare}}
<a href=#page=playlist&id={{this.id}}&action=download>
data-rel=popup
data-position-to=window
</a>
</li>
{{/each}}
</script>
......@@ -34,7 +39,7 @@
<input type="search" class="research" placeholder="research..."/>
<div data-role="content">
<ul data-role="listview" data-split-icon="delete" data-split-theme="d">
<ul data-role="listview" data-split-icon="arrow-d" data-split-theme="d">
</ul>
</div>
......
/*global window, rJS, RSVP, console, jQuery, $, JSON, Handlebars,
loopEventListener, RegExp */
loopEventListener, RegExp, ID3, FileAPIReader, Date */
/*jslint maxlen:80, nomen: true */
......@@ -10,6 +10,10 @@
.getElementById('network').innerHTML,
network = Handlebars.compile(network_source);
gk.declareAcquiredMethod("allDocs", "allDocs")
.declareAcquiredMethod("jio_putAttachment", "jio_putAttachment")
.declareAcquiredMethod("jio_post", "jio_post")
.declareAcquiredMethod("jio_remove", "jio_remove")
.declareAcquiredMethod("jio_getAttachment", "jio_getAttachment")
.declareAcquiredMethod("displayThisPage", "displayThisPage")
.declareAcquiredMethod("displayThisTitle", "displayThisTitle")
.declareAcquiredMethod("plEnablePage", "plEnablePage")
......@@ -27,10 +31,46 @@
]);
})
.push(function (param_list) {
var blob;
gadget.__element.getElementsByClassName('offline')[0]
.href = param_list[0];
gadget.__element.getElementsByClassName('online')[0]
.href = param_list[1];
if (options.action === "download") {
return gadget.jio_getAttachment({"_id" : options.id,
"_attachment" : "enclosure"})
.then(function (file) {
var now = new Date(),
type;
if (options.id.indexOf(".mp3") === -1) {
type = "video/webm";
} else {
type = "audio/mp3";
}
blob = file;
return gadget.jio_post({ "title" : options.id,
"type" : type,
"format" : type,
"size" : blob.size,
"artist" : "unknown",
"album" : "unknown",
"year" : "unknown",
"picture" : "./unknown.jpg",
"modified" : now.toUTCString(),
"date" : now.getFullYear() + "-" +
(now.getMonth() + 1) + "-"
+ now.getDate()
}, 0);
})
.then(function (res) {
gadget.putId = res.id;
return gadget.jio_putAttachment({
"_id" : res.id,
"_attachment" : "enclosure",
"_blob": blob
}, 0);
});
}
})
.push(function () {
return gadget.allDocs({"include_docs": true});
......@@ -46,7 +86,8 @@
}
return options.inverse(this);
});
if (options.id !== undefined && options.id !== "localhost") {
if (options.id !== undefined && options.id !== "localhost"
&& options.action !== "download") {
tmp = [];
for (i = 0, j = 0; i < e.data.rows.length; i += 1) {
exp = new RegExp(options.id, "i");
......@@ -66,6 +107,14 @@
})
.fail(function (error) {
if (!(error instanceof RSVP.CancellationError)) {
if (error.target.error.name === "QuotaExceededError") {
gadget.__element.getElementsByClassName('info')[0].innerHTML =
"QuotaError";
if (gadget.putId) {
return gadget.jio_remove({"_id": gadget.putId}, 0);
}
return;
}
gadget.__element.getElementsByClassName('info')[0].innerHTML =
"please enable local server";
}
......
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