Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, MinIO, Cloudflare R2, etc.) -->
+ AWS S3
Expand Down
16 changes: 15 additions & 1 deletion config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down Expand Up @@ -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.
Expand Down
16 changes: 15 additions & 1 deletion config.sample.zh-CN.toml
Original file line number Diff line number Diff line change
@@ -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`。

Expand Down Expand Up @@ -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。
Expand Down
2 changes: 1 addition & 1 deletion crates/upgit-uploaders/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions crates/upgit-uploaders/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
201 changes: 201 additions & 0 deletions crates/upgit-uploaders/src/gitlab.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
}

#[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<String, UploadError> {
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<Locator, UploadError> {
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<Locator, UploadError> {
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<Locator, UploadError> {
let data = read_bytes(artifact)?;
self.create_or_update(key, artifact, &data)
}
}
29 changes: 28 additions & 1 deletion crates/upgit-uploaders/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 },
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -318,6 +323,9 @@ fn resolved_kind(id: &str, profile: &UploaderProfile) -> Result<String, InstallE
if id == "github" || github_keys {
return Ok("github".to_string());
}
if id == "gitlab" {
return Ok("gitlab".to_string());
}
// Qiniu uses `bucket` (not bucket_name) and has no endpoint. Check before S3.
let qiniu_keys = has_field(profile, "access_key")
&& has_field(profile, "secret_key")
Expand Down Expand Up @@ -375,6 +383,25 @@ fn github_from_profile(
}))
}

fn gitlab_from_profile(
id: &str,
profile: &UploaderProfile,
) -> Result<GitlabUploader, InstallError> {
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<S3Uploader, InstallError> {
Ok(S3Uploader::new(S3Config {
region: require_string(id, profile, "region")?,
Expand Down
1 change: 1 addition & 0 deletions crates/upgit-uploaders/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading
Loading