2016-06-03 11:51:11 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
use mime_guess;
|
|
|
|
use std::io::{Seek, Read, SeekFrom};
|
|
|
|
use std::fs;
|
2016-09-04 23:44:46 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2016-06-03 11:51:11 +02:00
|
|
|
use page::handler;
|
|
|
|
use endpoint::{Endpoint, EndpointInfo, EndpointPath, Handler};
|
|
|
|
|
2016-09-09 20:00:55 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2016-06-03 11:51:11 +02:00
|
|
|
pub struct LocalPageEndpoint {
|
|
|
|
path: PathBuf,
|
2016-09-04 23:44:46 +02:00
|
|
|
mime: Option<String>,
|
|
|
|
info: Option<EndpointInfo>,
|
2016-06-03 11:51:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalPageEndpoint {
|
|
|
|
pub fn new(path: PathBuf, info: EndpointInfo) -> Self {
|
|
|
|
LocalPageEndpoint {
|
|
|
|
path: path,
|
2016-09-04 23:44:46 +02:00
|
|
|
mime: None,
|
|
|
|
info: Some(info),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn single_file(path: PathBuf, mime: String) -> Self {
|
|
|
|
LocalPageEndpoint {
|
|
|
|
path: path,
|
|
|
|
mime: Some(mime),
|
|
|
|
info: None,
|
2016-06-03 11:51:11 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 14:04:52 +02:00
|
|
|
|
|
|
|
pub fn path(&self) -> PathBuf {
|
|
|
|
self.path.clone()
|
|
|
|
}
|
2016-06-03 11:51:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Endpoint for LocalPageEndpoint {
|
|
|
|
fn info(&self) -> Option<&EndpointInfo> {
|
2016-09-04 23:44:46 +02:00
|
|
|
self.info.as_ref()
|
2016-06-03 11:51:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn to_handler(&self, path: EndpointPath) -> Box<Handler> {
|
2016-09-04 23:44:46 +02:00
|
|
|
if let Some(ref mime) = self.mime {
|
|
|
|
Box::new(handler::PageHandler {
|
|
|
|
app: LocalSingleFile { path: self.path.clone(), mime: mime.clone() },
|
|
|
|
prefix: None,
|
|
|
|
path: path,
|
2016-09-05 19:40:51 +02:00
|
|
|
file: Default::default(),
|
2016-09-04 23:44:46 +02:00
|
|
|
safe_to_embed: false,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Box::new(handler::PageHandler {
|
|
|
|
app: LocalDapp { path: self.path.clone() },
|
|
|
|
prefix: None,
|
|
|
|
path: path,
|
2016-09-05 19:40:51 +02:00
|
|
|
file: Default::default(),
|
2016-09-04 23:44:46 +02:00
|
|
|
safe_to_embed: false,
|
|
|
|
})
|
|
|
|
}
|
2016-06-03 11:51:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 23:44:46 +02:00
|
|
|
struct LocalSingleFile {
|
2016-06-03 11:51:11 +02:00
|
|
|
path: PathBuf,
|
2016-09-04 23:44:46 +02:00
|
|
|
mime: String,
|
2016-06-03 11:51:11 +02:00
|
|
|
}
|
|
|
|
|
2016-09-04 23:44:46 +02:00
|
|
|
impl handler::Dapp for LocalSingleFile {
|
|
|
|
type DappFile = LocalFile;
|
|
|
|
|
|
|
|
fn file(&self, _path: &str) -> Option<Self::DappFile> {
|
|
|
|
LocalFile::from_path(&self.path, Some(&self.mime))
|
2016-06-03 11:51:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 23:44:46 +02:00
|
|
|
struct LocalDapp {
|
|
|
|
path: PathBuf,
|
|
|
|
}
|
|
|
|
|
2016-06-03 11:51:11 +02:00
|
|
|
impl handler::Dapp for LocalDapp {
|
|
|
|
type DappFile = LocalFile;
|
|
|
|
|
|
|
|
fn file(&self, file_path: &str) -> Option<Self::DappFile> {
|
|
|
|
let mut path = self.path.clone();
|
|
|
|
for part in file_path.split('/') {
|
|
|
|
path.push(part);
|
|
|
|
}
|
2016-09-04 23:44:46 +02:00
|
|
|
LocalFile::from_path(&path, None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct LocalFile {
|
|
|
|
content_type: String,
|
|
|
|
buffer: [u8; 4096],
|
|
|
|
file: fs::File,
|
|
|
|
len: u64,
|
|
|
|
pos: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalFile {
|
|
|
|
fn from_path<P: AsRef<Path>>(path: P, mime: Option<&str>) -> Option<Self> {
|
2016-06-03 11:51:11 +02:00
|
|
|
// Check if file exists
|
2016-09-04 23:44:46 +02:00
|
|
|
fs::File::open(&path).ok().map(|file| {
|
|
|
|
let content_type = mime.map(|mime| mime.to_owned())
|
|
|
|
.unwrap_or_else(|| mime_guess::guess_mime_type(path).to_string());
|
2016-06-03 11:51:11 +02:00
|
|
|
let len = file.metadata().ok().map_or(0, |meta| meta.len());
|
|
|
|
LocalFile {
|
2016-09-04 23:44:46 +02:00
|
|
|
content_type: content_type,
|
2016-06-03 11:51:11 +02:00
|
|
|
buffer: [0; 4096],
|
|
|
|
file: file,
|
|
|
|
pos: 0,
|
|
|
|
len: len,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl handler::DappFile for LocalFile {
|
|
|
|
fn content_type(&self) -> &str {
|
|
|
|
&self.content_type
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_drained(&self) -> bool {
|
|
|
|
self.pos == self.len
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_chunk(&mut self) -> &[u8] {
|
|
|
|
let _ = self.file.seek(SeekFrom::Start(self.pos));
|
|
|
|
if let Ok(n) = self.file.read(&mut self.buffer) {
|
|
|
|
&self.buffer[0..n]
|
|
|
|
} else {
|
|
|
|
&self.buffer[0..0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bytes_written(&mut self, bytes: usize) {
|
|
|
|
self.pos += bytes as u64;
|
|
|
|
}
|
|
|
|
}
|