aboutsummaryrefslogtreecommitdiffstats
path: root/src/Controllers/DocumentsController.cs
blob: b2a6122137b0ea111825da095f40881c9d96a9ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using IOL.Helpers;
using Microsoft.AspNetCore.Mvc;
using VSH.Data;
using VSH.Data.Database;
using VSH.Data.Enums;
using VSH.Data.Static;
using VSH.Utilities;

namespace VSH.Controllers;

public class DocumentsController : MainControllerBase
{
	private readonly MainDbContext _context;

	public DocumentsController(MainDbContext context) {
		_context = context;
	}

	[HttpGet("{type}")]
	public ActionResult GetDocument(DocumentType type) {
		var document = _context.Documents.SingleOrDefault(d => d.Type == type);
		return Ok(document?.Content);
	}

	[HttpPost("{type}")]
	public ActionResult UpdateDocument([FromRoute] DocumentType type, [FromForm] string content) {
		var document = _context.Documents.SingleOrDefault(d => d.Type == type);
		if (document == default) {
			var newDocument = new Document {
					Content = content,
					Type = type,
					Created = DateTime.UtcNow
			};
			_context.Documents.Add(newDocument);
			_context.SaveChanges();
			return Ok(newDocument.Content);
		}

		document.Content = content;
		document.Updated = DateTime.UtcNow;
		_context.SaveChanges();
		return Ok(document.Content);
	}

	[HttpPost("upload-images")]
	public ActionResult UploadImages() {
		var files = Request.Form.Files;
		if (files.Count == 0)
			return BadRequest();
		var filePaths = new List<FileInfo>();
		var fileNames = new List<string>();
		foreach (var file in files) {
			var fileName = RandomString.Generate(4,
												 true)
						   + "_"
						   + file.FileName;
			var filePath = Path.Combine(AppPaths.DocumentImages.HostPath,
										fileName);
			using var writeStream = new FileStream(filePath,
												   FileMode.CreateNew);
			file.CopyTo(writeStream);
			filePaths.Add(new FileInfo(filePath));
			fileNames.Add(fileName);
		}

		foreach (var filePath in filePaths) {
			ImageFunctions.EnsureNormalOrLessImageResolution(filePath);
		}

		return Ok(fileNames);
	}
}