Skip to content

Commit

Permalink
add: file upload
Browse files Browse the repository at this point in the history
optimize: project structure
  • Loading branch information
XZB-1248 committed May 21, 2022
1 parent e878ea1 commit dae496d
Show file tree
Hide file tree
Showing 52 changed files with 1,439 additions and 865 deletions.
110 changes: 24 additions & 86 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- '!v*.*.*'

jobs:
build-macOS-clients:
build-clients-macOS:
runs-on: macos-latest

strategy:
Expand Down Expand Up @@ -51,7 +51,7 @@ jobs:


build-others:
needs: [ build-macOS-clients ]
needs: [ build-clients-macOS ]
runs-on: ubuntu-latest

strategy:
Expand All @@ -77,47 +77,42 @@ jobs:
export PATH=$PATH:~/go/bin/
go install github.com/rakyll/statik
- name: Get artifacts from previous job (arm64)
- name: Get artifact from previous job (darwin_arm64)
uses: actions/download-artifact@v3
with:
name: darwin_arm64
path: ./built

- name: Get artifacts from previous job (amd64)
- name: Get artifact from previous job (darwin_amd64)
uses: actions/download-artifact@v3
with:
name: darwin_amd64
path: ./built

- name: Build and embed clients
run: |
chmod +x ./build.client.sh
export GOMOD=`pwd`/go.mod
export CGO_ENABLED=0
go mod tidy
go mod download
./build.client.sh
statik -m -src="./built" -f -dest="./server/embed" -include=* -p built -ns built
- name: Build and pack static resources
run: |
cd ./web
npm install
npm run build-prod
statik -m -src="./dist" -f -dest="../server/embed" -p web -ns web
cd ..
zip -q -r ./embed.zip ./server/embed
- name: Build server
- name: Set up go dependencies
run: |
chmod +x ./build.server.sh
export GOMOD=`pwd`/go.mod
export CGO_ENABLED=0
go mod tidy
go mod download
- name: Build clients and servers
run: |
chmod +x ./scripts/build.client.sh
./scripts/build.client.sh
statik -m -src="./built" -f -dest="./server/embed" -include=* -p built -ns built
chmod +x ./scripts/build.server.sh
mkdir ./releases
./build.server.sh
./scripts/build.server.sh
- name: Prepare release note
run: |
Expand All @@ -128,6 +123,8 @@ jobs:
run: |
cd ./releases
sudo apt install zip tar -y
tar -zcvf server_darwin_arm64.tar.gz server_darwin_arm64
tar -zcvf server_darwin_amd64.tar.gz server_darwin_amd64
tar -zcvf server_linux_arm.tar.gz server_linux_arm
tar -zcvf server_linux_arm64.tar.gz server_linux_arm64
tar -zcvf server_linux_i386.tar.gz server_linux_i386
Expand All @@ -137,17 +134,13 @@ jobs:
zip -r server_windows_i386.zip server_windows_i386.exe
zip -r server_windows_amd64.zip server_windows_amd64.exe
- name: Upload embedding resources (embed.zip)
uses: actions/upload-artifact@v2
with:
name: embed.zip
path: embed.zip

- name: Release
uses: softprops/action-gh-release@v1
with:
body_path: CHANGELOG.md
files: |
releases/server_darwin_arm64.tar.gz
releases/server_darwin_amd64.tar.gz
releases/server_linux_arm.tar.gz
releases/server_linux_arm64.tar.gz
releases/server_linux_i386.tar.gz
Expand All @@ -157,64 +150,9 @@ jobs:
releases/server_windows_i386.zip
releases/server_windows_amd64.zip
build-macOS-servers:
needs: [ build-others ]
runs-on: macos-latest

strategy:
matrix:
go-version: [ 1.17 ]

steps:
- uses: actions/checkout@v3

- name: Set up golang
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

- name: Get artifacts from previous job (embed.zip)
uses: actions/download-artifact@v3
with:
name: embed.zip
path: .

- name: Build and compress servers
run: |
rm -rf ./server/embed
unzip -q embed.zip
export COMMIT=`git rev-parse HEAD`
export GOMOD=`pwd`/go.mod
export CGO_ENABLED=0
go mod tidy
go mod download
mkdir ./releases
export GOOS=darwin
export GOARCH=arm64
go build -ldflags "-s -w -X 'Spark/server/config.COMMIT=$COMMIT'" -tags=jsoniter -o ./releases/server_darwin_arm64 Spark/server
export GOARCH=amd64
go build -ldflags "-s -w -X 'Spark/server/config.COMMIT=$COMMIT'" -tags=jsoniter -o ./releases/server_darwin_amd64 Spark/server
cd ./releases
tar -zcvf server_darwin_arm64.tar.gz server_darwin_arm64
tar -zcvf server_darwin_amd64.tar.gz server_darwin_amd64
cd ..
- name: Release
uses: softprops/action-gh-release@v1
with:
files: |
releases/server_darwin_arm64.tar.gz
releases/server_darwin_amd64.tar.gz
- name: Clean up
uses: geekyeggo/delete-artifact@v1
with:
name: |
embed.zip
darwin_arm64
darwin_amd64
- name: Clean up
uses: geekyeggo/delete-artifact@v1
with:
name: |
darwin_arm64
darwin_amd64
73 changes: 64 additions & 9 deletions API.ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@
## 通用

所有请求均为`POST`

<br />
每次请求都必须在Header中带上`Authorization`

<br />
`Authorization`请求头格式:`Basic <token>`(basic auth)。

```
Authorization: Basic <base64('username:password')>
```
例如:
```
Authorization: Basic WFpCOjEyNDg=
```

---

## 响应

所有响应均是JSON格式。

<br />
`code` 有三种结果,分别为`-1``0``1`,含义如下。

| code | meaning |
Expand Down Expand Up @@ -55,9 +59,13 @@ Authorization: Basic <base64('username:password')>

参数:****

设备的`id`是一串64位的字符串,每台设备独一无二,一般不会变化。识别设备主要靠这个。下文中提到的设备ID也指的是这个。

每个device对象所对应的key,是它的本次连接的连接ID,这个ID是随机、临时的,每次重连就会变化,不建议使用。
设备的`id`是一串64位的字符串,每台设备独一无二,一般不会变化。
<br />
识别设备主要靠这个。下文中提到的设备ID也指的是这个。
<br />
每个device对象所对应的key,是它的本次连接的连接ID。
<br />
连接ID是随机、临时的,每次重连就会变化,不建议使用。

```
{
Expand Down Expand Up @@ -123,7 +131,9 @@ Authorization: Basic <base64('username:password')>

参数:`device`(设备ID)

如果截屏获取成功,则会直接以图片的形式输出。如果截屏失败,如下响应会被输出(错误信息不止这一个)。
如果截屏获取成功,则会直接以图片的形式输出。
<br />
如果截屏失败,如下响应会被输出(错误信息不一定是这一个)。

```
{
Expand All @@ -138,7 +148,9 @@ Authorization: Basic <base64('username:password')>

参数:`file`(文件路径) 以及 `device`(设备ID)

如果文件存在且可访问,则文件会直接输出。否则,会给出以下响应。
如果文件存在且可访问,则文件会直接输出。
<br />
否则,会给出错误原因。

```
{
Expand Down Expand Up @@ -169,11 +181,54 @@ Authorization: Basic <base64('username:password')>

---

### 上传文件到目录:`/device/file/upload`

**GET**参数:`file`(文件名)、`path`(路径)和`device`(设备ID)

文件内容需要作为**请求体body**发送。
<br />
**请求体body**中的任何内容都会被写到指定地文件中。
<br />
如果存在同名文件,则会被**覆盖**

Example:
```http request
POST http://localhost:8000/api/device/file/upload?path=D%3A%5C&file=Test.txt&device=bc7e49f8f794f80ffb0032a4ba516c86d76041bf2023e1be6c5dda3b1ee0cf4c HTTP/1.1
Host: localhost:8000
Content-Length: 12
Content-Type: application/octet-stream
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47
Origin: http://localhost:8000
Referer: http://localhost:8000/
Hello World.
```

如果文件上传成功,则`code``1`
<br />
文件`D:\Test.txt`会写入:`Hello World.`

```
{
"code": 0
}
```
```
{
"code": 1,
"msg": "${i18n|fileOrDirNotExist}"
}
```

---

### 列举设备上的文件和目录:`/device/file/list`

参数:`path`(父目录路径) 以及 `device`(设备ID)

如果`path`为空,windows下会给出磁盘列表,其它系统会默认输出`/`目录下的文件和目录。
如果`path`为空,windows下会给出磁盘列表。
<br />
其它系统会默认输出`/`目录下的文件和目录。

`type`有三种结果:`0`代表文件,`1`代表目录,`2`代表磁盘(windows)。

Expand Down
57 changes: 53 additions & 4 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
## Common

Only `POST` requests are allowed.

<br />
For every request, you should have `Authorization` on its header.

<br />
Authorization header is a string like `Basic <token>`(basic auth).

```
Authorization: Basic <base64('username:password')>
```
Example:
```
Authorization: Basic WFpCOjEyNDg=
```

---

Expand Down Expand Up @@ -53,8 +57,12 @@ All responses are JSON encoded.

Parameters: **None**

The `id` of device is persistent, its length always equals 64. It's unique for every device and won't change, so you should identify every device by this.

The `id` of device is persistent, its length always equals 64.
<br />
It's unique for every device and won't change.
<br />
You're recommend to recognize your device by device ID.
<br />
The key of the device object is its connection UUID, it's random and temporary.

```
Expand Down Expand Up @@ -166,6 +174,47 @@ If file exists and is deleted successfully, then `code` will be `0`.

---

### Upload file: `/device/file/upload`

**Query Parameters**: `file` (file name), `path` and `device` (device ID)

File itself should be sent in the request **body**.
<br />
**Anything** represented in the request **body** will be saved to the device.
<br />
If same file exists, then it will be **overwritten**.

Example:
```http request
POST http://localhost:8000/api/device/file/upload?path=D%3A%5C&file=Test.txt&device=bc7e49f8f794f80ffb0032a4ba516c86d76041bf2023e1be6c5dda3b1ee0cf4c HTTP/1.1
Host: localhost:8000
Content-Length: 12
Content-Type: application/octet-stream
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47
Origin: http://localhost:8000
Referer: http://localhost:8000/
Hello World.
```

If file uploaded successfully, then `code` will be `0`.
<br />
And `D:\Test.txt` will be created with the content of `Hello World.`.

```
{
"code": 0
}
```
```
{
"code": 1,
"msg": "${i18n|fileOrDirNotExist}"
}
```

---

### List files: `/device/file/list`

Parameters: `path` (folder to be listed) and `device` (device ID)
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## v0.0.8

* Add: file upload.
* Optimize: project structure.

* 新增: 文件上传功能。
* 优化: 项目结构。



## v0.0.7

* Add: detail info tooltip of cpu, ram and disk.
Expand Down
Loading

0 comments on commit dae496d

Please sign in to comment.