Put Blob URL online

This commit is contained in:
Zhen Zhang 2016-07-15 01:02:21 +08:00
parent 4b78b9adab
commit fdc3a8e3ac
17 changed files with 260 additions and 191 deletions

View file

@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use filemanager_thread::FileOrigin;
use std::str::FromStr;
use url::Url;
use uuid::Uuid;
@ -20,7 +21,7 @@ pub enum BlobURLStoreError {
}
/// Standalone blob buffer object
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BlobBuf {
pub filename: Option<String>,
/// MIME type string
@ -33,13 +34,25 @@ pub struct BlobBuf {
/// Parse URL as Blob URL scheme's definition
/// https://w3c.github.io/FileAPI/#DefinitionOfScheme
pub fn parse_blob_url(url: &Url) -> Option<(Uuid, Option<&str>)> {
url.path_segments().and_then(|mut segments| {
let id_str = match (segments.next(), segments.next()) {
(Some(s), None) => s,
_ => return None,
};
Uuid::from_str(id_str).map(|id| (id, url.fragment())).ok()
})
pub fn parse_blob_url(url: &Url) -> Result<(Uuid, FileOrigin, Option<String>), ()> {
let url_inner = try!(Url::parse(url.path()).map_err(|_| ()));
let fragment = url_inner.fragment().map(|s| s.to_string());
let mut segs = try!(url_inner.path_segments().ok_or(()));
let id = try!(segs.nth(0).ok_or(()));
let id = try!(Uuid::from_str(id).map_err(|_| ()));
Ok((id, get_blob_origin(&url_inner), fragment))
}
/// Given an URL, returning the Origin that a Blob created under this
/// URL should have.
/// HACK(izgzhen): Not well-specified on spec, and it is a bit a hack
/// both due to ambiguity of spec and that we have to serialization the
/// Origin here.
pub fn get_blob_origin(url: &Url) -> FileOrigin {
if url.scheme() == "file" {
// NOTE: by default this is "null" (Opaque), which is not ideal
"file://".to_string()
} else {
url.origin().unicode_serialization()
}
}