Skip to content
This repository was archived by the owner on Jul 27, 2024. It is now read-only.

Commit b21899d

Browse files
committed
Added dotnet attachment upload example
1 parent 95c80d7 commit b21899d

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

Diff for: dotnet-upload-attachment/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
build/
3+
obj/
4+
bin/

Diff for: dotnet-upload-attachment/BookStackConsole.sln

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BookStackConsole", "BookStackConsole\BookStackConsole.csproj", "{BA24248C-5A62-427B-8D67-5CB3386FF3B8}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{BA24248C-5A62-427B-8D67-5CB3386FF3B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{BA24248C-5A62-427B-8D67-5CB3386FF3B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{BA24248C-5A62-427B-8D67-5CB3386FF3B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{BA24248C-5A62-427B-8D67-5CB3386FF3B8}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

Diff for: dotnet-upload-attachment/BookStackConsole/Program.cs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
using System.Net.Http;
5+
6+
namespace BookStackConsole
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
// Check expected command arguments have been passed
13+
if (args.Length < 2)
14+
{
15+
Console.Error.WriteLine("Both <page_id> and <file_path> need to be provided!");
16+
Environment.Exit(1);
17+
}
18+
19+
// Get our BookStack details from the environment
20+
var baseUrl = Environment.GetEnvironmentVariable("BS_URL") ?? "";
21+
var tokenId = Environment.GetEnvironmentVariable("BS_TOKEN_ID") ?? "";
22+
var tokenSecret = Environment.GetEnvironmentVariable("BS_TOKEN_SECRET") ?? "";
23+
baseUrl = baseUrl.TrimEnd('/');
24+
Console.WriteLine("base: " + baseUrl);
25+
26+
// Get our target page ID and file path from command args.
27+
var pageId = args[0];
28+
var filePath = args[1];
29+
30+
// Check our file exists
31+
if (!File.Exists(filePath))
32+
{
33+
Console.Error.WriteLine("Both <page_id> and <file_path> need to be provided!");
34+
Environment.Exit(1);
35+
}
36+
37+
// Get our file name and read stream
38+
var fileName = Path.GetFileName(filePath);
39+
var fileStream = File.OpenRead(filePath);
40+
41+
// Format our post data
42+
var postData = new MultipartFormDataContent();
43+
postData.Add(new StringContent(pageId), "uploaded_to");
44+
postData.Add(new StringContent(fileName), "name");
45+
postData.Add(new StreamContent(fileStream), "file", fileName);
46+
47+
// Attempt to send up our file
48+
var client = new HttpClient();
49+
client.DefaultRequestHeaders.Add("Authorization", $"Token {tokenId}:{tokenSecret}");
50+
var respMessage = client.PostAsync(baseUrl + "/api/attachments", postData);
51+
52+
// Write out a message to show success/failure along with response data
53+
Console.WriteLine("Response: " + respMessage.Result.Content.ReadAsStringAsync().Result);
54+
if (respMessage.IsCompletedSuccessfully && respMessage.Result.StatusCode == HttpStatusCode.OK)
55+
{
56+
Console.WriteLine("Attachment uploaded successfully!");
57+
Environment.Exit(0);
58+
}
59+
60+
Console.WriteLine("Attachment failed to upload!");
61+
Environment.Exit(1);
62+
}
63+
}
64+
}

Diff for: dotnet-upload-attachment/readme.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Upload a file attachment to a BookStack page
2+
3+
This project will produce an executable "BookStackConsole" binary
4+
that takes a path to any local file and attempt
5+
to upload it to a BookStack page as an attachment
6+
using the API using a multipart/form-data request.
7+
8+
**This is very simplistic and has been written with very little c#/.net knowledge, it is only mean to serve as a working example.**
9+
10+
## Requirements
11+
12+
You will need .NET installed (Tested on .NET 5.0 on Fedora 35 Linux).
13+
14+
## Running
15+
16+
First, download all the files in the same directory as this readme to a folder on your system
17+
and run the below from within that directory.
18+
19+
```bash
20+
# Setup
21+
# ALTERNATIVELY: Open the program.cs file and add to the empty strings in the variables near the top.
22+
export BS_URL=https://bookstack.example.com # Set to be your BookStack base URL
23+
export BS_TOKEN_ID=abc123 # Set to be your API token_id
24+
export BS_TOKEN_SECRET=123abc # Set to be your API token_secret
25+
26+
# Build with dotnet
27+
dotnet build
28+
29+
# Running the script
30+
./BookStackConsole/bin/Debug/net5.0/BookStackConsole <page_id> <file_path>
31+
```
32+
33+
- `<page_id>` - The ID of the page you want to upload the attachment to.
34+
- `<file_path>` - File you want to upload as an attachment.
35+
36+
## Examples
37+
38+
```bash
39+
# Upload the 'cat-image-collection.zip' file as an attachment to page of ID 205
40+
./BookStackConsole/bin/Debug/net5.0/BookStackConsole 205 ./cat-image-collection.zip
41+
```

0 commit comments

Comments
 (0)