-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesController.cs
More file actions
129 lines (111 loc) · 5.54 KB
/
FilesController.cs
File metadata and controls
129 lines (111 loc) · 5.54 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System.IO;
using System.Security.Claims;
using System.Threading.Tasks;
using FileSystemCommon;
using FileSystemCommon.Models.FileSystem.Files;
using FileSystemWeb.Extensions.Http;
using FileSystemWeb.Helpers;
using FileSystemWeb.Models;
using FileSystemWeb.Models.RequestBodies;
using FileSystemWeb.Services.File;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
namespace FileSystemWeb.Controllers.API
{
[Route("api/[controller]")]
public class FilesController : ControllerBase
{
private readonly FileService fileService;
public FilesController(FileService fileService)
{
this.fileService = fileService;
}
[HttpGet("")]
[HttpGet("{encodedVirtualPath}")]
public async Task<ActionResult> Get(string encodedVirtualPath, [FromQuery] string path)
{
string userId = HttpContext.GetUserId();
string virtualPath = PathHelper.DecodeAndValidatePath(encodedVirtualPath ?? path);
InternalFile file = await fileService.Get(userId, virtualPath);
string fileName = Utils.ReplaceNonAscii(file.Name);
Response.Headers.Add(HeaderNames.ContentDisposition, $"inline; filename=\"{fileName}\"");
string contentType = Utils.GetContentType(Path.GetExtension(file.Name));
return PhysicalFile(file.PhysicalPath, contentType, true);
}
[HttpGet("download")]
[HttpGet("{encodedVirtualPath}/download")]
public async Task<ActionResult> Download(string encodedVirtualPath, [FromQuery] string path)
{
string userId = HttpContext.GetUserId();
string virtualPath = PathHelper.DecodeAndValidatePath(encodedVirtualPath ?? path);
InternalFile file = await fileService.Get(userId, virtualPath);
string contentType = Utils.GetContentType(Path.GetExtension(file.Name));
return PhysicalFile(file.PhysicalPath, contentType, file.Name);
}
[HttpGet("exists")]
[HttpGet("{encodedVirtualPath}/exists")]
public async Task<ActionResult<bool>> Exists(string encodedVirtualPath, [FromQuery] string path)
{
string userId = HttpContext.GetUserId();
string virtualPath = PathHelper.DecodeAndValidatePath(encodedVirtualPath ?? path);
return await fileService.Exists(userId, virtualPath);
}
[HttpGet("info")]
[HttpGet("{encodedVirtualPath}/info")]
public async Task<ActionResult<FileItemInfo>> GetInfo(string encodedVirtualPath, [FromQuery] string path)
{
string userId = HttpContext.GetUserId();
string virtualPath = PathHelper.DecodeAndValidatePath(encodedVirtualPath ?? path);
return await fileService.GetInfo(userId, virtualPath);
}
[HttpGet("hash")]
[HttpGet("{encodedVirtualPath}/hash")]
public async Task<ActionResult<string>> GetHash(string encodedVirtualPath, [FromQuery] string path, [FromQuery] int partialSize)
{
string userId = HttpContext.GetUserId();
string virtualPath = PathHelper.DecodeAndValidatePath(encodedVirtualPath ?? path);
return await fileService.GetHash(userId, virtualPath, partialSize);
}
[HttpPost("copy")]
[HttpPost("{encodedVirtualSrcPath}/{encodedVirtualDestPath}/copy")]
public async Task<ActionResult> Copy(string encodedVirtualSrcPath, string encodedVirtualDestPath, [FromQuery] string srcPath, [FromQuery] string destPath)
{
string userId = HttpContext.GetUserId();
string virtualSrcPath = PathHelper.DecodeAndValidatePath(encodedVirtualSrcPath ?? srcPath);
string virtualDestPath = PathHelper.DecodeAndValidatePath(encodedVirtualDestPath ?? destPath);
await fileService.Copy(userId, virtualSrcPath, virtualDestPath);
return Ok();
}
[HttpPost("move")]
[HttpPost("{encodedVirtualSrcPath}/{encodedVirtualDestPath}/move")]
public async Task<ActionResult> Move(string encodedVirtualSrcPath, string encodedVirtualDestPath, [FromQuery] string srcPath, [FromQuery] string destPath)
{
string userId = HttpContext.GetUserId();
string virtualSrcPath = PathHelper.DecodeAndValidatePath(encodedVirtualSrcPath ?? srcPath);
string virtualDestPath = PathHelper.DecodeAndValidatePath(encodedVirtualDestPath ?? destPath);
await fileService.Move(userId, virtualSrcPath, virtualDestPath);
return Ok();
}
[DisableRequestSizeLimit]
[HttpPost]
[HttpPost("{encodedVirtualPath}")]
public async Task<ActionResult> Write(string encodedVirtualPath, [FromQuery] string path, [FromForm] WriteFileBody form)
{
if (form?.FileContent == null) return BadRequest("Missing file content");
string userId = HttpContext.GetUserId();
string virtualPath = PathHelper.DecodeAndValidatePath(encodedVirtualPath ?? path);
await fileService.Write(userId, virtualPath, form.FileContent);
return Ok();
}
[HttpDelete("")]
[HttpDelete("{encodedVirtualPath}")]
public async Task<ActionResult> Delete(string encodedVirtualPath, [FromQuery] string path)
{
string userId = HttpContext.GetUserId();
string virtualPath = PathHelper.DecodeAndValidatePath(encodedVirtualPath ?? path);
await fileService.Delete(userId, virtualPath);
return Ok();
}
}
}