2016-06-08 14:13:39 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
|
|
|
|
2016-07-15 16:39:23 +03:00
|
|
|
use filemanager_thread::FileOrigin;
|
2016-11-18 00:34:47 +03:00
|
|
|
use servo_url::ServoUrl;
|
2016-06-08 14:13:39 +03:00
|
|
|
use std::str::FromStr;
|
|
|
|
use url::Url;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2016-07-13 08:00:08 +03:00
|
|
|
/// Errors returned to Blob URL Store request
|
2017-08-24 01:18:31 +03:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2016-06-08 14:13:39 +03:00
|
|
|
pub enum BlobURLStoreError {
|
2016-07-04 19:15:23 +03:00
|
|
|
/// Invalid File UUID
|
|
|
|
InvalidFileID,
|
2016-06-08 14:13:39 +03:00
|
|
|
/// Invalid URL origin
|
|
|
|
InvalidOrigin,
|
2016-07-09 06:33:26 +03:00
|
|
|
/// Invalid entry content
|
|
|
|
InvalidEntry,
|
|
|
|
/// External error, from like file system, I/O etc.
|
2016-07-13 08:00:08 +03:00
|
|
|
External(String),
|
2016-06-08 14:13:39 +03:00
|
|
|
}
|
|
|
|
|
2016-07-13 08:00:08 +03:00
|
|
|
/// Standalone blob buffer object
|
2017-08-24 01:18:31 +03:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
2016-07-13 08:00:08 +03:00
|
|
|
pub struct BlobBuf {
|
|
|
|
pub filename: Option<String>,
|
2016-06-08 14:13:39 +03:00
|
|
|
/// MIME type string
|
|
|
|
pub type_string: String,
|
|
|
|
/// Size of content in bytes
|
|
|
|
pub size: u64,
|
|
|
|
/// Content of blob
|
|
|
|
pub bytes: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse URL as Blob URL scheme's definition
|
2017-10-18 00:04:10 +03:00
|
|
|
///
|
|
|
|
/// <https://w3c.github.io/FileAPI/#DefinitionOfScheme>
|
2017-01-30 17:21:46 +03:00
|
|
|
pub fn parse_blob_url(url: &ServoUrl) -> Result<(Uuid, FileOrigin), ()> {
|
2017-06-18 15:55:11 +03:00
|
|
|
let url_inner = Url::parse(url.path()).map_err(|_| ())?;
|
2016-11-18 00:34:47 +03:00
|
|
|
let id = {
|
2017-06-18 15:55:11 +03:00
|
|
|
let mut segs = url_inner.path_segments().ok_or(())?;
|
|
|
|
let id = segs.nth(0).ok_or(())?;
|
|
|
|
Uuid::from_str(id).map_err(|_| ())?
|
2016-11-18 00:34:47 +03:00
|
|
|
};
|
2017-01-30 17:21:46 +03:00
|
|
|
Ok((id, get_blob_origin(&ServoUrl::from_url(url_inner))))
|
2016-07-15 16:39:23 +03:00
|
|
|
}
|
2016-06-08 14:13:39 +03:00
|
|
|
|
2016-07-15 16:39:23 +03:00
|
|
|
/// Given an URL, returning the Origin that a Blob created under this
|
|
|
|
/// URL should have.
|
2017-10-18 00:04:10 +03:00
|
|
|
///
|
2016-07-15 16:39:23 +03:00
|
|
|
/// 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.
|
2016-11-18 00:34:47 +03:00
|
|
|
pub fn get_blob_origin(url: &ServoUrl) -> FileOrigin {
|
2016-07-15 16:39:23 +03:00
|
|
|
if url.scheme() == "file" {
|
|
|
|
// NOTE: by default this is "null" (Opaque), which is not ideal
|
|
|
|
"file://".to_string()
|
|
|
|
} else {
|
|
|
|
url.origin().unicode_serialization()
|
|
|
|
}
|
2016-06-08 14:13:39 +03:00
|
|
|
}
|