diff --git a/README.md b/README.md index 572c9b0..4839e2e 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ This is also useful with [Typora](https://support.typora.io/Upload-Image/#image- ### Supported Upload Extensions + Github ++ GitLab + S3 Compatible Storages + AWS S3 diff --git a/config.sample.toml b/config.sample.toml index 6a5db10..7a92aea 100644 --- a/config.sample.toml +++ b/config.sample.toml @@ -2,7 +2,7 @@ # GitHub is the default. Copy one table into config.toml and set `default` to its name. # Packed/init config is GitHub-only: config.github.toml in the upgit repository. # HTTP hosts: a table named after a file in recipes/ needs no `type = "http"`. -# Built-in types (github, s3, aliyunoss, qcloudcos, upyun, qiniu) use their real fields. +# Built-in types (github, gitlab, s3, aliyunoss, qcloudcos, upyun, qiniu) use their real fields. # `default_uploader` is an alias of `default`. `rename` is an alias of `naming`. # `prefix` is an alias of Qiniu `public_base`. `replacements` is an alias of `link`. @@ -35,6 +35,20 @@ repo = "repo-name" # Branch to commit into. Must match the repository default branch. branch = "main" +# GitLab (self-hosted or gitlab.com). Private projects make raw URLs 404 unless public_base is set. +[uploaders.gitlab] +# GitLab instance origin (alias: host). No trailing slash; not the /api/v4 URL. +url = "https://gitlab.example.com" +# Project path with namespace (group/name). Encoded on the wire as group%2Fname. +project = "group/name" +# Personal access token with api or write_repository. Sent as PRIVATE-TOKEN. +token = "YOUR_GITLAB_TOKEN" +# Branch to commit into. Defaults to main if omitted. +branch = "main" +# Optional public URL prefix for printed links (CDN, reverse proxy, or a public project raw prefix). +# Private GitLab `/-/raw/` URLs 404 without this. +# public_base = "https://cdn.example.com/" + # SM.MS image host. Token: https://sm.ms/home/apitoken [uploaders.smms] # SM.MS API token from the dashboard. diff --git a/config.sample.zh-CN.toml b/config.sample.zh-CN.toml index d17e724..67726e5 100644 --- a/config.sample.zh-CN.toml +++ b/config.sample.zh-CN.toml @@ -1,7 +1,7 @@ # 仓库里的完整示例,不会被 `upgit init` 写入。 # 默认是 GitHub。把某一段 [uploaders.*] 拷进 config.toml,并把 `default` 改成该表名。 # HTTP 图床:表名与 recipes/ 里的文件名相同即可,不必写 `type = "http"`。 -# 内置类型(github、s3、aliyunoss、qcloudcos、upyun、qiniu)填写各自的真实字段。 +# 内置类型(github、gitlab、s3、aliyunoss、qcloudcos、upyun、qiniu)填写各自的真实字段。 # `default_uploader` 是 `default` 的别名。`rename` 是 `naming` 的别名。 # 七牛的 `prefix` 等价于 `public_base`。`replacements` 等价于 `link`。 @@ -34,6 +34,20 @@ repo = "仓库名" # 提交到的分支。必须与仓库默认分支一致。 branch = "main" +# GitLab(自建或 gitlab.com)。私有项目的 raw 直链会 404,除非设置 public_base。 +[uploaders.gitlab] +# GitLab 实例 origin(别名 host)。不要末尾斜杠,不要填 /api/v4。 +url = "https://gitlab.example.com" +# 带命名空间的项目路径(group/name)。请求里会编码成 group%2Fname。 +project = "group/name" +# 个人访问令牌,需要 api 或 write_repository。请求头是 PRIVATE-TOKEN。 +token = "YOUR_GITLAB_TOKEN" +# 提交到的分支。省略则默认 main。 +branch = "main" +# 可选的公开 URL 前缀(CDN、反代,或公开项目的 raw 前缀),用于打印出的链接。 +# 私有 GitLab 的 `/-/raw/` 直链没有这项会 404。 +# public_base = "https://cdn.example.com/" + # SM.MS:https://sm.ms/home/apitoken [uploaders.smms] # SM.MS 控制台里的 API token。 diff --git a/crates/upgit-uploaders/Cargo.toml b/crates/upgit-uploaders/Cargo.toml index becc037..53020c6 100644 --- a/crates/upgit-uploaders/Cargo.toml +++ b/crates/upgit-uploaders/Cargo.toml @@ -5,7 +5,7 @@ edition.workspace = true license.workspace = true repository.workspace = true publish.workspace = true -description = "Uploader objects for upgit: GitHub, S3, OSS, COS, Upyun, Qiniu, HTTP recipes." +description = "Uploader objects for upgit: GitHub, GitLab, S3, OSS, COS, Upyun, Qiniu, HTTP recipes." [dependencies] base64 = "0.22" diff --git a/crates/upgit-uploaders/src/catalog.rs b/crates/upgit-uploaders/src/catalog.rs index 9ff12e4..422345e 100644 --- a/crates/upgit-uploaders/src/catalog.rs +++ b/crates/upgit-uploaders/src/catalog.rs @@ -19,6 +19,10 @@ impl HostCatalog { id: "github", title: "GitHub", }, + Host { + id: "gitlab", + title: "GitLab", + }, Host { id: "s3", title: "Amazon S3 (MinIO / Cloudflare R2 / Backblaze / Wasabi / DigitalOcean Spaces / Ceph / Flexify.IO / IBM Cloud Object Storage)", diff --git a/crates/upgit-uploaders/src/gitlab.rs b/crates/upgit-uploaders/src/gitlab.rs new file mode 100644 index 0000000..1dd6e5f --- /dev/null +++ b/crates/upgit-uploaders/src/gitlab.rs @@ -0,0 +1,201 @@ +use base64::engine::general_purpose::STANDARD; +use base64::Engine; +use serde::Serialize; +use upgit_core::{Artifact, Locator, ObjectKey, UploadError, Uploader}; + +use crate::util::{ + could_not_reach, host_of, http_origin, join_host_path, json_string_field, + looks_like_rate_limit, percent_encode, read_bytes, +}; + +#[derive(Serialize)] +struct FileBody<'a> { + branch: &'a str, + content: String, + encoding: &'a str, + commit_message: String, +} + +#[derive(Debug, Clone)] +pub struct GitlabConfig { + pub url: String, + pub project: String, + pub token: String, + pub branch: String, + pub public_base: Option, +} + +#[derive(Debug, Clone)] +pub struct GitlabUploader { + config: GitlabConfig, +} + +impl GitlabUploader { + pub fn new(mut config: GitlabConfig) -> Self { + if config.branch.trim().is_empty() { + config.branch = "main".to_string(); + } + config.url = http_origin(&config.url); + config.project = config.project.trim().trim_matches('/').to_string(); + config.public_base = config + .public_base + .map(|base| http_origin(&base)) + .filter(|s| !s.is_empty()); + Self { config } + } + + pub fn locator_for(&self, key: &ObjectKey) -> Locator { + if let Some(base) = &self.config.public_base { + Locator::new(join_host_path(base, key.as_str())) + } else { + Locator::new(format!( + "{}/{}/-/raw/{}/{}", + self.config.url, + self.config.project, + self.config.branch, + key.as_str() + )) + } + } + + /// Map a GitLab Repository Files API status + body to a user-facing error. + /// Never dumps JSON. + pub fn explain(&self, status: u16, body: &str) -> UploadError { + let project = self.config.project.as_str(); + match status { + 401 => UploadError::new( + "GitLab", + "GitLab personal access token is invalid or expired (HTTP 401).", + format!( + "Create a token with the \"api\" or \"write_repository\" scope at {}/-/user_settings/personal_access_tokens and put it in [uploaders.gitlab] `token`.", + self.config.url + ), + Some(status), + ), + 429 => Self::rate_limit_error(status), + 403 if looks_like_rate_limit(body) => Self::rate_limit_error(status), + 403 => UploadError::new( + "GitLab", + format!("GitLab denied access to `{project}` (HTTP 403)."), + format!( + "The token in [uploaders.gitlab] `token` lacks access to `{project}`. Grant the \"api\" or \"write_repository\" scope and a Developer role on this project." + ), + Some(status), + ), + 404 => UploadError::new( + "GitLab", + format!("GitLab project `{project}` was not found (HTTP 404)."), + "Check [uploaders.gitlab] url, project, and token. A missing project, or a token without access, also returns 404. Private projects make GitLab raw URLs 404; set public_base to a CDN, reverse proxy, or public raw prefix.", + Some(status), + ), + 400 | 409 | 422 => UploadError::new( + "GitLab", + format!("GitLab cannot create or update that path (HTTP {status})."), + "Check [uploaders.gitlab] branch and the file path.", + Some(status), + ), + 500..=599 => UploadError::new( + "GitLab", + format!("GitLab is failing (HTTP {status})."), + "Retry later; this is a GitLab server error, not a config problem.", + Some(status), + ), + _ => { + let what = match json_string_field(body, "message") { + Some(msg) => format!("GitLab upload failed (HTTP {status}): {msg}"), + None => format!("GitLab upload failed (HTTP {status})."), + }; + UploadError::new( + "GitLab", + what, + "Verify [uploaders.gitlab] url, project, token, and branch. Private projects make GitLab raw URLs 404; set public_base.", + Some(status), + ) + } + } + } + + fn files_url(&self, key: &ObjectKey) -> String { + format!( + "{}/api/v4/projects/{}/repository/files/{}", + self.config.url, + percent_encode(&self.config.project), + percent_encode(key.as_str()), + ) + } + + fn encode_file_body(&self, file_name: &str, data: &[u8]) -> Result { + serde_json::to_string(&FileBody { + branch: &self.config.branch, + content: STANDARD.encode(data), + encoding: "base64", + commit_message: format!("upload {file_name} via upgit"), + }) + .map_err(|e| UploadError::message(e.to_string())) + } + + fn file_already_exists(status: u16, body: &str) -> bool { + status == 400 && body.to_ascii_lowercase().contains("already exists") + } + + fn rate_limit_error(status: u16) -> UploadError { + UploadError::new( + "GitLab", + format!("GitLab rate limit exceeded (HTTP {status})."), + "Wait and retry, or use a token with a higher rate limit in [uploaders.gitlab] `token`.", + Some(status), + ) + } + + fn authorized(&self, req: ureq::Request) -> ureq::Request { + req.set("PRIVATE-TOKEN", &self.config.token) + .set("User-Agent", "upgit") + } + + fn create_or_update( + &self, + key: &ObjectKey, + artifact: &Artifact, + data: &[u8], + ) -> Result { + let body = self.encode_file_body(artifact.file_name(), data)?; + let url = self.files_url(key); + match self + .authorized(ureq::post(&url)) + .set("Content-Type", "application/json") + .send_string(&body) + { + Ok(_) => Ok(self.locator_for(key)), + Err(ureq::Error::Status(code, resp)) => { + let text = resp.into_string().unwrap_or_default(); + if Self::file_already_exists(code, &text) { + return self.update_file(&url, &body, key); + } + Err(self.explain(code, &text)) + } + Err(e) => Err(could_not_reach("GitLab", host_of(&self.config.url), e)), + } + } + + fn update_file(&self, url: &str, body: &str, key: &ObjectKey) -> Result { + match self + .authorized(ureq::put(url)) + .set("Content-Type", "application/json") + .send_string(body) + { + Ok(_) => Ok(self.locator_for(key)), + Err(ureq::Error::Status(code, resp)) => { + let text = resp.into_string().unwrap_or_default(); + Err(self.explain(code, &text)) + } + Err(e) => Err(could_not_reach("GitLab", host_of(&self.config.url), e)), + } + } +} + +impl Uploader for GitlabUploader { + fn upload(&self, artifact: &Artifact, key: &ObjectKey) -> Result { + let data = read_bytes(artifact)?; + self.create_or_update(key, artifact, &data) + } +} diff --git a/crates/upgit-uploaders/src/install.rs b/crates/upgit-uploaders/src/install.rs index 4459336..ce6bb71 100644 --- a/crates/upgit-uploaders/src/install.rs +++ b/crates/upgit-uploaders/src/install.rs @@ -9,6 +9,7 @@ use upgit_core::{KeyPolicy, KeyPolicyError, Registry}; use crate::catalog::RecipeCatalog; use crate::cos::{CosConfig, CosUploader}; use crate::github::{GithubConfig, GithubUploader}; +use crate::gitlab::{GitlabConfig, GitlabUploader}; use crate::oss::{OssConfig, OssUploader}; use crate::qiniu::{QiniuConfig, QiniuUploader}; use crate::recipe::{HttpRecipe, HttpRecipeUploader, RecipeError}; @@ -27,7 +28,7 @@ pub enum InstallError { MissingField { id: String, field: String }, #[error("uploader `{id}` still has a static Qiniu upload token, which expires. Set access_key, secret_key, bucket, and public_base instead.")] ExpiredQiniuToken { id: String }, - #[error("unknown uploader type `{kind}` for `{id}`. Built-in types: github, s3, aliyunoss, qcloudcos, upyun, qiniu.")] + #[error("unknown uploader type `{kind}` for `{id}`. Built-in types: github, gitlab, s3, aliyunoss, qcloudcos, upyun, qiniu.")] UnknownKind { id: String, kind: String }, #[error("unknown http recipe `{recipe}` for `{id}`")] UnknownRecipe { id: String, recipe: String }, @@ -220,6 +221,10 @@ impl AppConfig { let uploader = github_from_profile(id, profile)?; registry.register(id.clone(), Box::new(uploader)); } + "gitlab" => { + let uploader = gitlab_from_profile(id, profile)?; + registry.register(id.clone(), Box::new(uploader)); + } "s3" => { let uploader = s3_from_profile(id, profile)?; registry.register(id.clone(), Box::new(uploader)); @@ -318,6 +323,9 @@ fn resolved_kind(id: &str, profile: &UploaderProfile) -> Result Result { + Ok(GitlabUploader::new(GitlabConfig { + url: optional_string(profile, "url") + .or_else(|| optional_string(profile, "host")) + .filter(|s| !is_placeholder(s)) + .ok_or_else(|| InstallError::MissingField { + id: id.to_string(), + field: "url".to_string(), + })?, + project: require_string(id, profile, "project")?, + token: require_string(id, profile, "token")?, + branch: optional_string(profile, "branch").unwrap_or_default(), + public_base: optional_string(profile, "public_base").filter(|s| !is_placeholder(s)), + })) +} + fn s3_from_profile(id: &str, profile: &UploaderProfile) -> Result { Ok(S3Uploader::new(S3Config { region: require_string(id, profile, "region")?, diff --git a/crates/upgit-uploaders/src/lib.rs b/crates/upgit-uploaders/src/lib.rs index a041ee5..dba85fb 100644 --- a/crates/upgit-uploaders/src/lib.rs +++ b/crates/upgit-uploaders/src/lib.rs @@ -2,6 +2,7 @@ mod catalog; pub mod cos; mod form; pub mod github; +pub mod gitlab; mod install; pub mod oss; pub mod qiniu; diff --git a/crates/upgit-uploaders/src/util.rs b/crates/upgit-uploaders/src/util.rs index f331713..6135b5c 100644 --- a/crates/upgit-uploaders/src/util.rs +++ b/crates/upgit-uploaders/src/util.rs @@ -158,6 +158,40 @@ pub(crate) fn host_of(url: &str) -> &str { rest.trim_end_matches('/') } +/// Absolute HTTP URL: add `https://` if no scheme, strip surrounding slashes. +pub(crate) fn http_origin(url: &str) -> String { + let t = url.trim(); + let (scheme, rest) = if let Some(rest) = t.strip_prefix("https://") { + ("https://", rest) + } else if let Some(rest) = t.strip_prefix("http://") { + ("http://", rest) + } else if t.is_empty() { + return String::new(); + } else { + ("https://", t) + }; + let rest = rest.trim_matches('/'); + if rest.is_empty() { + String::new() + } else { + format!("{scheme}{rest}") + } +} + +/// RFC 3986 unreserved stay literal; everything else is `%HH` (uppercase). +pub(crate) fn percent_encode(s: &str) -> String { + let mut out = String::new(); + for &b in s.as_bytes() { + match b { + b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => { + out.push(b as char) + } + _ => out.push_str(&format!("%{b:02X}")), + } + } + out +} + pub(crate) fn one_line(s: &str, max: usize) -> String { let line = s.lines().next().unwrap_or("").trim(); let n = line.chars().count(); diff --git a/docs/README.zh-CN.md b/docs/README.zh-CN.md index f957f06..bbb88d6 100644 --- a/docs/README.zh-CN.md +++ b/docs/README.zh-CN.md @@ -29,6 +29,7 @@ ### 上传扩展 + Github ++ GitLab + S3 兼容存储 + AWS S3 + MinIO