GNU General Public License v3.0

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

TanneReynolds/ShareX-Upload-Server 深度技术分析

项目背景与痛点

在当今的信息时代,文件的上传与分享变得越来越重要。许多服务允许用户直接上传文件,但这些服务往往缺乏稳定性和自定义的能力。ShareX-Upload-Server 正是为了填补这一空白而诞生的,它提供的不仅仅是上传功能,而是集成了多种功能特性的稳定服务端,基于 Node.js 构建。它不仅支持图片、视频、代码、文本和 Markdown 渲染,还可通过 Discord 集成进行日志记录和管理,同时支持 URL 缩短和全前端界面。这一切功能的集合使它成为了专业开发者的理想选择,尤其是在需要自定义服务和功能高度集成的环境中。

核心技术揭秘

技术架构与实现原理

ShareX-Upload-Server 的架构包含前端与后端两大部分,采用 Node.js 作为后台服务框架,这为实现高并发和低延迟提供了保障。前端部分则通过 React 和其他现代前端框架实现,提供了用户友好的界面。

背后设计思想

该项目设计旨在提供一个功能丰富且稳定的文件和 ShareX 上传服务器。通过丰富功能集的集成,如图片、视频、代码、文本、Markdown 渲染,以及 URL 缩短等功能,它不仅提供了基础的上传服务,而且增强了用户体验,使其更为方便地进行文件管理和分享。

核心技术栈
  • Node.js:提供高性能的 I/O 操作,支撑高并发的日志记录和用户上传需求,同时支持异步非阻塞操作。
  • Express.js:轻量级路由层,便于后端逻辑处理和模块化业务分离。
  • React:成熟的前端框架,提升用户体验,提供简洁美观的界面设计。
  • WebSockets:实现实时通讯与数据同步,保证用户能在上传过程中实时查看上传进度。
  • Discord.js:集成 Discord,便于团队管理和通知,使开发及运营过程更为高效。
技术难点与设计模式

数据流与处理逻辑如下:

graph TD A[用户请求上传] --> B[服务器接收] B --> C{"文件类型校验"} C -->|"通过"| D[存储并处理] C -->|"失败"| E[返回错误信息] D --> F[生成URL] F --> G[返回前端]

项目通过日志记录及用户通知功能完成了日志方面的技术挑战,确保在发生错误或特殊事件时能即时通知到维护人员。同时,通过 WebSockets 实时接收上传状态,用户可即时了解上传进度,这不仅增强了用户体验,也提高了系统的透明度。

功能亮点与差异

相较同类服务,ShareX-Upload-Server 的功能亮点在于全方位的集成能力与自定义选项,其不仅具备基本的上传与分享功能,而且以 Discord 集成为典型特色。通过集成 Discord,ShareX-Upload-Server 实现了管理与通知功能,允许管理员通过 Discord 进行管理和状态更新,这在同类服务中较为罕见。

此外,它提供的覆盖完备的 API 接口设计与前端界面,轻松适应不同场景。通过 API,开发者可以根据需求进行扩展和定制,或者通过前端界面进行自定义操作。这带来了极大的灵活性和可扩展性。

应用场景与落地建议

在生产环境中使用 ShareX-Upload-Server 时,需要注意接口的安全性配置及权限管理。必须确保防火墙已正确配置,以防止潜在的安全威胁。对于确保服务的长期稳定运行,建议定期进行性能调优与更新以适应不断变化的技术环境。

适用场景

适用于在技术社区进行代码、文档等分享的场景。也适用于多团队协作项目,通过集成 Discord,简化管理过程,提供日志追踪与状态更新。此外,适用于需要自定义用户交互和复杂操作的直交且定制上传方式,其灵活性和集成特性尤其亮眼。

综合评价

优点

  • 自定义与灵活性:支持自定义服务器操作,可根据需求扩展功能和服务。
  • 完备的日志与通知:通过集成到第三方软件(如 Discord)进行更有效地管理与通知。
  • 高性能与高并发:Node.js 提供的高性能支持可以满足大量用户同时上传的需求。

缺点

  • 依赖较多:提供诸多功能的同时也增加了依赖服务或库的管理难度。
  • 集成复杂:对于不熟悉 Discord API 的用户来说,集成与管理可能具有较高的学习曲线。

总的来说,ShareX-Upload-Server 是一个功能丰富、高度灵活且具有开放性的文件服务端项目。然而,对于需要进行综合性整合开发的开发者来说,理解和掌握项目的技术细节是关键。通过充分利用其特性,可以创建强大且高效的文件分享平台,极大地提升开发者的生产力和协同工作效率。

关于

GitHub 原始描述(英文)

AKA ShareS - Feature full & Stable ShareX and file server in node. Includes images, videos, code, text, markdown rendering, password protected uploads, logging via discord, administration through Discord, url shortening, and a full front end. Use standalone or via reverse proxy

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

语言占比

CSS
Dockerfile
JavaScript
Shell

README

ShareS - A Nodejs ShareX Upload Server

Join Our Discord!

Features

  • Image/Video/General file uploading

  • Text (With Syntax Highlighting) [Example]

  • URL shortening + a front end for the URL shortener as well

  • Markdown rendering files [Example]

  • Logging via a Discord channel

  • Password protected uploading (Uploads that require file-specific password to view/download)

  • Server Administration using Discord bot commands

  • Front end upload page [Example]

  • Showcase images (image display pages that show metadata for photography) [Example]

Installation (Ubuntu 16.04 Server)

git clone https://github.com/TannerReynolds/ShareX-Upload-Server.git
cd ShareX-Upload-Server
chmod +x install.sh
./install.sh

Docker

docker build -t sharex-upload-server .
docker run --name "sharex-upload-server" -d \
    -v $(pwd)/src/config.json:/usr/src/app/config.json \
    -v $(pwd)/src/db.json:/usr/src/app/db.json \
    -v $(pwd)/src/server/uploads/:/usr/src/app/server/uploads/ \
    -p 8000:80 -p 8443:443 \
    sharex-upload-server
docker logs -f sharex-upload-server

/src/config.json will be the config file used if you used the above command to start the server. The web UI will be available on https://server-ip:8443 if https is enabled, and http://server-ip:8000 if it isn’t.

Configuration

In the files you downloaded from this repository, you will see a file called config.json You must fill this out for the webserver to work properly. Below explains the configuration file and what each part does

{
  "key": [""], // Password(s) for private uploading
  "domain": "*.example.com", // Domain server will use. Will error if domain not used in request. Place "*" as the subdomain to enable wildcard subdomains for the webserver.
  "puploadKeyGenLength": 64, // Amount of characters server should use for pupload files
  "public": false, // Disables auth and does not render a password field for /upload
  "maxUploadSize": 50, // max upload size for non-admins using regular key in MB
  "markdown": true, // enables markdown rendering (upload whole .md file for render)
  "port": 80, // port to listen on
  "secure": true, // Whether or not you want https. (make sure key and cert.pem are in src directory)
  "fileNameLength": 4, // File name length
  "shortUrlLength": 3, // File name length for short URLs
  "securePort": 443, // Port to use when secure is true
  "ratelimit": 1000, // Ratelimit for POSTing in milliseconds
  "dateURLPath": false, // Set to true to prefix uploads with the date (Ex: https://domain.com/2020/04/22/ghNa.pdf)
  "allowed":[
    "png", "jpg", "gif", "mp4", "mp3", "jpeg", "tiff", "bmp", "ico", "psd", "eps", "raw", "cr2", "nef", "sr2", "orf", "svg", "wav", "webm", "aac", "flac", "ogg", "wma", "m4a", "gifv"
  ], // Allowed file types for non-admins
  "admin":{
    "key": [""], // Admin password(s) for uploading & for gallery access
    "maxUploadSize": 1024, // Max upload size for admin in MB
    "allowed": [
    "png", "jpg", "gif", "mp4", "mp3","jpeg", "tiff", "bmp", "ico", "psd", "eps", "raw", "cr2", "nef", "sr2", "orf", "svg", "wav", "webm", "aac", "flac", "ogg", "wma", "m4a", "gifv", "html"
     ] // Allowed file types for admins
  },
  "paste": {
    "maxUploadSize": 20 // allowed paste upload size in MB
  },
  "discordToken": "", // Discord bot token
  "discordAdminIDs": ["discord IDs of people who can run commands go here", "Like this"], // User IDs in an array
  "discordChannelID": "", // Channel ID for monitoring uploads to
  "prefix": "" // Bot Prefix
}

Running The Server

Once you’ve properly configured your server, you can run node index.js in the src folder to start the server. You can keep your server running forever if you use a process manager, like pm2. pm2 installs along with your server if you used the install.sh script to install your server. Otherwise you can run npm i -g pm2 to install pm2. Then you can run your server by running pm2 start index.js, and monitor logs and such using pm2 monit

Note: Nginx/reverse proxy users

If you’re configuring this webserver to run through an Nginx reverse proxy, make sure you add these lines to your reverse proxy config

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;

This is generally some things you want to add to your config, and is what’s actually required for ShareS to work properly. This is because ShareS returns uploads like [http/https]://[requested url]/[filename] and since you’re running ShareS through a reverse proxy, unless you’re passing along the original headers, ShareS is most likely just going to send you something like http://[server's real ip address]/[filename]

Note: Users of multiple domains

If you have multiple domains pointed to this webserver, only one (can include wildcard subdomain) can be used, unless the domain setting is set to just a single * like so: "domain": "*",. This means that any domain will be accepted as a valid domain by the server, regardless of subdomain.

Setting up Discord logging

if you wish to log your webserver’s activity in a Discord channel for whatever reason, you can. Here is information on how to setup a bot account and get the information needed for Discord logging

Configuring Your ShareX Client

Configuring for Password Protected Uploading

  • Add a field to your body called pupload, and then make the value whatever you want the password to be
  • Upload something, and the upload will give you a url to the authentication page
  • Type in your password, and it will display/download the file!
Auto Password Generation

In addition to being able to use any password you want for puploads, if you type in *random* as your pupload field, the server will automatically generate a password for you. This password will include letters, numbers, and special characters. It will generate a key based on the length you specify in your config (puploadKeyGenLength). When making requests, the server will return the image URL with the key like so URL: https://qoilo.com/lhHr | KEY: Np$[CBk>X[c^YY{MDlCHH0|Qfm1uK0*lld^Mi$f4d62R5x6C2>~yaL}3*QYnziuZ

Showcase Field

  • Add a field to your body called showCase, and then make the value true
  • Upload an image
  • Click the image to view image’s metadata like camera, lens, iso, shutter speed, etc.
  • Requires extra software to be installed to your server, called Exiftool to read metadata from uploaded images. The install file will automatically install this software on ubuntu.
  • Windows servers using this feature wil need the Windows executable for exiftool and it will need to be added to your environment variables or ShareS will throw errors on upload and return 404s

Using with Flameshot (Linux)

In order to use ShareS with Flameshot you will need to use a simple script, here is an example:

key="YourPassword"
# Only needed for multi-domain support, if you only have one simply set the url
# 2 lines below to url="https://your.domain/api/files"
urls=("https://example.com/api/files" "https://example.org/api/files")
url=${urls[$RANDOM % ${#urls[@]}]}

temp_file="/tmp/screenshot.png"

# Run flameshot --help for options
flameshot gui -r > $temp_file

# For some reason flameshot always seems to exit with 0 even when aborting the process
# so we had to find a way around that.
if [[ $(file --mime-type -b $temp_file) != "image/png" ]]; then
	rm $temp_file
  notify-send "Screenshot aborted" -a "Flameshot" && exit 1
fi

image_url=$(curl -X POST -F "fdata=@"$temp_file -F "key="$key -v "$url" 2>/dev/null)
echo -n $image_url | xclip -sel c
notify-send "Image URL copied to clipboard" "$image_url" -a "Flameshot" -i $temp_file
rm $temp_file

When running this script simply hit enter when you’re satisfied with your image, Flameshot will then save the image to your clipboard which will then be replaced with the image URL once it’s uploaded. For the best results I suggest disabling notifications in the Flameshot app.

Contributing

Pull Requests

  • Be sure you properly lint your files prior to making a pull request. eslint file available
  • Do your own testing
  • Properly comment your code. My code isn’t really commented on because it’s my code and I understand how everything fits together and works. It isn’t a very large project. You will need to comment on your code though so that I could understand going forward when more features get added
  • Not all features are going to be added. Just because you code something that may be useful to you, that does not mean it will for sure be added.
  • Update the version of ShareS when you create a pull request. If the current version at the time of your PR is 4.5.6, make sure to change the shield in the README and the package.json file to say version 4.5.7. If the version is 4.5.10, then update it to 4.5.11. Only update the last digit. Updates/PRs that are not code (README.md, install.sh, sxcu files, package.json, etc.) should not constitute an updated version number for the project, so PRs for those specific files will not increment the version number

Bug Reports

  • Follow the issue template format or your issue will likely not be solved. If you do not follow the format I won’t have enough information to diagnose the problem and fix it.
  • If I ask questions and you do not respond on the thread, and I’m not experiencing the issue / am not able to replicate the issue, I will close the issue thread, and you’ll have to make another one if the problem persists.
  • Please make sure your system setup isn’t the cause of your issue. This webserver can be ran through several different pieces of software, reverse proxies, operating systems, network types, just please make sure that you havent made in error in setting up your system before submitting the issue. We do not help people with every combination of setup that exists, if you’re using some weird OS, runnning ShareS through your own custom reverse proxy, don’t expect much support from us if it doesn’t work.
  • Ensure your config isn’t the issue. Most issues people have with setting up the server and experiencing crashes are due to an error in how you made your config file. Be sure to read the descriptions for each one and use the correct data type (array, string, number, array with strings, etc.)

Credits

Ken - Initial File Uploader
Aetheryx - Webserver Structure
Jaex - ShareX
Cheap Hosting Options For Your Uploader

评论

暂无评论