Browse Source

refactor(pri_config.sh): 重构上传功能并优化 memo 函数

- 重构了 upload_file_common 函数,增加了用户名和密码参数,支持 HTTP Basic Auth
- 更新了 upload_file 和 upload_tmp_file 函数,使用新的上传 URL 和凭证
- 优化了 memo 函数,移除了未使用的代码并简化了逻辑
- 删除了 todo 函数的多余空行
tianyun 5 months ago
parent
commit
1587f6fa60
2 changed files with 47 additions and 74 deletions
  1. 47 25
      pri_config.sh
  2. 0 49
      sh_config.sh

+ 47 - 25
pri_config.sh

@@ -2,18 +2,18 @@ memo() {
     # 获取传入的参数
     # 获取传入的参数
     updatedContent=$*
     updatedContent=$*
     # 转义特殊字符
     # 转义特殊字符
-#    escapedContent=$(echo -n "$updatedContent" | jq -sRr @uri)
+    #    escapedContent=$(echo -n "$updatedContent" | jq -sRr @uri)
 
 
     # 构建请求体数据
     # 构建请求体数据
-#    data="{\"content\": \"$escapedContent\"}"
+    #    data="{\"content\": \"$escapedContent\"}"
     # 构建请求体数据
     # 构建请求体数据
     data="{\"content\": \"$updatedContent\"}"
     data="{\"content\": \"$updatedContent\"}"
 
 
     # 发送请求
     # 发送请求
     response=$(curl -s -X POST -H "Content-Type: application/json" \
     response=$(curl -s -X POST -H "Content-Type: application/json" \
-    -H "Authorization: bearer eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoidGlhbnl1bnBlcmZlY3QiLCJpc3MiOiJtZW1vcyIsInN1YiI6IjEiLCJhdWQiOlsidXNlci5hY2Nlc3MtdG9rZW4iXSwiaWF0IjoxNzA5MTc5NTUyfQ.LFxWB4efya1sL7VoJ42xpXxbAip-udT_Kx2OwZ8Y3-E" \
-    -d "$data" \
-    "https://memos.tianyunperfect.cn/api/v1/memo")
+        -H "Authorization: bearer eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoidGlhbnl1bnBlcmZlY3QiLCJpc3MiOiJtZW1vcyIsInN1YiI6IjEiLCJhdWQiOlsidXNlci5hY2Nlc3MtdG9rZW4iXSwiaWF0IjoxNzA5MTc5NTUyfQ.LFxWB4efya1sL7VoJ42xpXxbAip-udT_Kx2OwZ8Y3-E" \
+        -d "$data" \
+        "https://memos.tianyunperfect.cn/api/v1/memo")
 
 
     # 判断是否存在name字段
     # 判断是否存在name字段
     if [[ $response == *"name"* ]]; then
     if [[ $response == *"name"* ]]; then
@@ -22,16 +22,20 @@ memo() {
         echo "失败" $response
         echo "失败" $response
     fi
     fi
 }
 }
-todo(){
-  memo "#todo $*"
+todo() {
+    memo "#todo $*"
 }
 }
 
 
-
-
 # 通用上传函数
 # 通用上传函数
 function upload_file_common() {
 function upload_file_common() {
+    # 参数1: 文件路径
     local file_path="$1"
     local file_path="$1"
+    # 参数2: 上传URL(例如:http://x.xxx.cn:44704/2025/)
     local upload_url="$2"
     local upload_url="$2"
+    # 参数3: 用户名(可选,用于HTTP Basic Auth)
+    local username="${3:-admin}"
+    # 参数4: 密码(可选,用于HTTP Basic Auth)
+    local password="${4:-123456}"
 
 
     # 检查文件路径是否为空
     # 检查文件路径是否为空
     if [ -z "$file_path" ]; then
     if [ -z "$file_path" ]; then
@@ -45,36 +49,54 @@ function upload_file_common() {
         return 1
         return 1
     fi
     fi
 
 
-    # 使用curl上传文件并获取响应
-    local response=$(curl -k -X POST -F "file=@$file_path" "$upload_url")
-    echo $response
+    # 检查上传URL是否为空
+    if [ -z "$upload_url" ]; then
+        echo "Error: upload_url is empty!"
+        return 1
+    fi
+
+    # 获取文件名
+    local file_name=$(basename "$file_path")
+
+    # 构造完整的上传目标URL
+    local full_upload_url="${upload_url%/}/$file_name"
 
 
-    # 解析JSON响应,提取path字段
-    local path1=$(echo "$response" | jq -r '.path')
+    # 使用curl上传文件
+    echo "Uploading file '$file_name' to '$full_upload_url'..."
+    local response=$(curl -u "$username:$password" -T "$file_path" "$upload_url" 2>&1)
 
 
-    if [ -z "$path1" ]; then
-        echo "Error: Failed to get the path from the response!"
+    # 检查curl命令是否成功执行
+    if [ $? -ne 0 ]; then
+        echo "Error: Failed to upload file. Response: $response"
         return 1
         return 1
     fi
     fi
 
 
-    echo ''
-    echo "https://alist.tianyunperfect.cn/oss_jing1/$path1"
-    echo "https://kedaoyun.tianyunperfect.cn/#explorer&pathFile=%7Bio:5%7D/$path1"
-    echo "https://oss_b_1.tianyunperfect.cn/$path1"
-    echo "https://tianyunperfect1.oss-cn-beijing.aliyuncs.com/$path1"
+    # 构造下载地址
+    local download_url="${upload_url%/}/$file_name"
+    download_url="http://${username}:${password}@${download_url#http://}"
+
+    # 打印下载地址
+    echo "File uploaded successfully!"
+    echo "Download URL: $download_url"
 }
 }
 
 
 # 上传文件到指定的URL
 # 上传文件到指定的URL
 function upload_file() {
 function upload_file() {
-    upload_file_common "$1" "https://web_history.tianyunperfect.cn/oss/upload_file"
+    # 获取当前年份
+    local current_year=$(date +"%Y")
+
+    # 构造上传URL,使用当前年份替换固定的2025
+    local upload_url="http://nas1.tianyunperfect.cn:44704/${current_year}/"
+
+    # 调用通用上传函数
+    upload_file_common "$1" "$upload_url" "admin" "tianyunperfect"
 }
 }
 
 
 # 上传临时文件到指定的URL
 # 上传临时文件到指定的URL
 function upload_tmp_file() {
 function upload_tmp_file() {
-    upload_file_common "$1" "https://web_history.tianyunperfect.cn/oss/upload_tmp_file"
+    upload_file_common "$1" "http://nas1.tianyunperfect.cn:44704/upload_tmp_file/" "admin" "tianyunperfect"
 }
 }
 
 
-
 # 获取本机IP地址
 # 获取本机IP地址
 get_ip_address() {
 get_ip_address() {
     ip_address=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | head -n 1)
     ip_address=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | head -n 1)
@@ -96,4 +118,4 @@ unset_proxy() {
     echo "$unset_command"
     echo "$unset_command"
     eval "$unset_command"
     eval "$unset_command"
     echo "Proxy has been unset"
     echo "Proxy has been unset"
-}
+}

+ 0 - 49
sh_config.sh

@@ -70,42 +70,6 @@ EOF
 
 
 }
 }
 
 
-function install_zellij(){
-    # 用户目录是否存在 zellij 文件
-    if [ ! -f ~/zellij ]; then
-        wget https://tianyunperfect1.oss-cn-beijing.aliyuncs.com/file/2024/11/zellij-x86_64-unknown-linux-musl.tar.gz
-        tar -zxvf zellij-x86_64-unknown-linux-musl.tar.gz
-        rm -rf zellij-x86_64-unknown-linux-musl.tar.gz
-
-        if [ -n "$ZSH_VERSION" ]; then
-            echo "正在使用zsh"
-            cat >> ~/.zshrc << \EOF
-# 方便快速进入
-function zin(){
-  export ZELLIJ_AUTO_ATTACH=true
-  eval "$(~/zellij setup --generate-auto-start zsh)"
-}
-EOF
-            source ~/.zshrc
-
-        elif [ -n "$BASH_VERSION" ]; then
-            echo "正在使用bash"
-            cat >> ~/.bashrc << \EOF
-# 方便快速进入
-function zin(){
-  export ZELLIJ_AUTO_ATTACH=true
-  eval "$(~/zellij setup --generate-auto-start bash)"
-}
-EOF
-            source ~/.bashrc
-        else
-            echo "无法确定当前 Shell 类型"
-        fi
-
-        echo "OK。zin!!!"
-    fi
-}
-
 # 安装监控用户命令
 # 安装监控用户命令
 function install_monitor_user_command(){
 function install_monitor_user_command(){
         if [ -n "$ZSH_VERSION" ]; then
         if [ -n "$ZSH_VERSION" ]; then
@@ -125,19 +89,6 @@ function install_yy(){
     x env use yazi                          # 安装 yazi
     x env use yazi                          # 安装 yazi
 }
 }
 
 
-yy() {
-    local _yy_tmp=; local _yy_cwd=
-    _yy_tmp="$(command mktemp -t "yazi-cwd.XXXXXX")"
-    command yazi "$@" --cwd-file="$_yy_tmp"
-
-    _yy_cwd="$(cat -- "$_yy_tmp")"
-    command rm -f -- "$_yy_tmp"
-
-    if [ -n "$_yy_cwd" ] && [ "$_yy_cwd" != "$PWD" ]; then
-        command cd -- "$_yy_cwd"
-    fi
-}
-
 
 
 function k8s-get-all-namespace(){
 function k8s-get-all-namespace(){
 	kubectl get namespace
 	kubectl get namespace