6 namespace BookStackConsole
10 static void Main(string[] args)
12 // Check expected command arguments have been passed
15 Console.Error.WriteLine("Both <page_id> and <file_path> need to be provided!");
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);
26 // Get our target page ID and file path from command args.
28 var filePath = args[1];
30 // Check our file exists
31 if (!File.Exists(filePath))
33 Console.Error.WriteLine("Both <page_id> and <file_path> need to be provided!");
37 // Get our file name and read stream
38 var fileName = Path.GetFileName(filePath);
39 var fileStream = File.OpenRead(filePath);
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);
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);
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)
56 Console.WriteLine("Attachment uploaded successfully!");
60 Console.WriteLine("Attachment failed to upload!");