Skip to content

Commit

Permalink
Merge pull request #216 from messense/read-only
Browse files Browse the repository at this point in the history
增加只读模式
  • Loading branch information
messense authored Dec 23, 2021
2 parents 4330c6a + 324c6c5 commit 3200f59
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions openwrt/aliyundrive-webdav/files/aliyundrive-webdav.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ config server
option root '/'
option no_trash '0'
option domain_id ''
option read_only '0'
7 changes: 7 additions & 0 deletions openwrt/aliyundrive-webdav/files/aliyundrive-webdav.init
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ start_service() {
;;
*) ;;
esac

case "$(uci_get_by_type server read_only 0)" in
1|on|true|yes|enabled)
extra_options="$extra_options --read-only"
;;
*) ;;
esac
fi

procd_open_instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ cache_ttl.datatype = "uinteger"
no_trash = e:option(Flag, "no_trash", translate("Delete file permanently instead of trashing"))
no_trash.rmempty = false

read_only = e:option(Flag, "read_only", translate("Enable read only mode"))
read_only.description = translate("Disallow upload, modify and delete file operations")
read_only.rmempty = false

domain_id = e:option(Value, "domain_id", translate("Domain ID"))
domain_id.description = translate("Input domain_id option will use <a href=\"https://www.aliyun.com/product/storage/pds\" target=\"_blank\">Aliyun PDS</a> instead of <a href=\"https://www.aliyundrive.com\" target=\"_blank\">AliyunDrive</a>")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ msgstr "限制只能访问该云盘目录,默认为 / 表示不限制,注意
msgid "Delete file permanently instead of trashing"
msgstr "删除文件不放入回收站"

msgid "Enable read only mode"
msgstr "启用只读模式"

msgid "Disallow upload, modify and delete file operations"
msgstr "禁止上传、修改和删除文件操作"

msgid "Domain ID"
msgstr "阿里云相册与云盘服务 domainId"

Expand Down
26 changes: 18 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ struct Opt {
/// Aliyun PDS domain id
#[structopt(long)]
domain_id: Option<String>,
/// Enable read only mode
#[structopt(long)]
read_only: bool,
}

#[tokio::main(flavor = "multi_thread")]
Expand Down Expand Up @@ -104,14 +107,21 @@ async fn main() -> anyhow::Result<()> {
.map_err(|_| {
io::Error::new(io::ErrorKind::Other, "initialize aliyundrive client failed")
})?;
let fs = AliyunDriveFileSystem::new(drive, opt.root, opt.cache_size, opt.cache_ttl, no_trash)
.await
.map_err(|_| {
io::Error::new(
io::ErrorKind::Other,
"initialize aliyundrive file system failed",
)
})?;
let fs = AliyunDriveFileSystem::new(
drive,
opt.root,
opt.cache_size,
opt.cache_ttl,
no_trash,
opt.read_only,
)
.await
.map_err(|_| {
io::Error::new(
io::ErrorKind::Other,
"initialize aliyundrive file system failed",
)
})?;
debug!("aliyundrive file system initialized");

let dav_server = DavHandler::builder()
Expand Down
31 changes: 31 additions & 0 deletions src/vfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct AliyunDriveFileSystem {
uploading: Arc<DashMap<String, Vec<AliyunFile>>>,
root: PathBuf,
no_trash: bool,
read_only: bool,
}

impl AliyunDriveFileSystem {
Expand All @@ -40,6 +41,7 @@ impl AliyunDriveFileSystem {
cache_size: usize,
cache_ttl: u64,
no_trash: bool,
read_only: bool,
) -> Result<Self> {
let dir_cache = Cache::new(cache_size, cache_ttl);
debug!("dir cache initialized");
Expand All @@ -54,6 +56,7 @@ impl AliyunDriveFileSystem {
uploading: Arc::new(DashMap::new()),
root,
no_trash,
read_only,
})
}

Expand Down Expand Up @@ -210,6 +213,10 @@ impl DavFileSystem for AliyunDriveFileSystem {
if options.write && options.create_new {
return Err(FsError::Exists);
}
if options.write && self.read_only {
return Err(FsError::Forbidden);
}

if let Some(size) = options.size {
// 上传中的文件刚开始 size 可能为 0,更新为正确的 size
if file.size == 0 {
Expand All @@ -218,6 +225,10 @@ impl DavFileSystem for AliyunDriveFileSystem {
}
AliyunDavFile::new(self.clone(), file, parent_file.id)
} else if options.write && (options.create || options.create_new) {
if self.read_only {
return Err(FsError::Forbidden);
}

let size = options.size;
let name = dav_path
.file_name()
Expand Down Expand Up @@ -282,6 +293,10 @@ impl DavFileSystem for AliyunDriveFileSystem {
let path = self.normalize_dav_path(dav_path);
debug!(path = %path.display(), "fs: create_dir");
async move {
if self.read_only {
return Err(FsError::Forbidden);
}

let parent_path = path.parent().ok_or(FsError::NotFound)?;
let parent_file = self
.get_file(parent_path.to_path_buf())
Expand Down Expand Up @@ -312,6 +327,10 @@ impl DavFileSystem for AliyunDriveFileSystem {
let path = self.normalize_dav_path(dav_path);
debug!(path = %path.display(), "fs: remove_dir");
async move {
if self.read_only {
return Err(FsError::Forbidden);
}

let file = self
.get_file(path.clone())
.await?
Expand All @@ -336,6 +355,10 @@ impl DavFileSystem for AliyunDriveFileSystem {
let path = self.normalize_dav_path(dav_path);
debug!(path = %path.display(), "fs: remove_file");
async move {
if self.read_only {
return Err(FsError::Forbidden);
}

let file = self
.get_file(path.clone())
.await?
Expand All @@ -361,6 +384,10 @@ impl DavFileSystem for AliyunDriveFileSystem {
let to = self.normalize_dav_path(to_dav);
debug!(from = %from.display(), to = %to.display(), "fs: copy");
async move {
if self.read_only {
return Err(FsError::Forbidden);
}

let file = self
.get_file(from.clone())
.await?
Expand Down Expand Up @@ -390,6 +417,10 @@ impl DavFileSystem for AliyunDriveFileSystem {
let to = self.normalize_dav_path(to_dav);
debug!(from = %from.display(), to = %to.display(), "fs: rename");
async move {
if self.read_only {
return Err(FsError::Forbidden);
}

if from.parent() == to.parent() {
// rename
if let Some(name) = to.file_name() {
Expand Down

0 comments on commit 3200f59

Please sign in to comment.