Skip to content

Commit ae2dcdd

Browse files
committed
Initial commit
1 parent 5043fca commit ae2dcdd

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

Server-side/Controllers/PostgresDocumentStorageController.cs

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -41,58 +41,6 @@ public async Task<IActionResult> HandleFileManagerOperationsAsync([FromBody] Fil
4141
};
4242
}
4343

44-
/// <summary>
45-
/// Downloads one or more documents. Zips multiple files.
46-
/// </summary>
47-
[HttpPost("downloadAsync")]
48-
public async Task<IActionResult> DownloadAsync([FromForm] string downloadInput)
49-
{
50-
if (string.IsNullOrWhiteSpace(downloadInput))
51-
return BadRequest("Missing download input");
52-
53-
var args = JsonConvert.DeserializeObject<FileManagerDirectoryContent>(downloadInput);
54-
55-
if (args.Data == null || args.Data.Length == 0)
56-
return BadRequest("No files to download.");
57-
58-
// Single file case
59-
if (args.Data.Length == 1)
60-
{
61-
var fileItem = args.Data[0];
62-
if (!int.TryParse(fileItem.Id, out int id))
63-
return BadRequest("Invalid file ID.");
64-
65-
var document = await _documentContext.Documents.FirstOrDefaultAsync(d => d.Id == id);
66-
if (document == null || document.FileData == null || document.FileData.Length == 0)
67-
return NotFound("File not found or is empty.");
68-
69-
var contentType = GetContentType(document.Name);
70-
return File(document.FileData, contentType, document.Name);
71-
}
72-
73-
// Multiple files - zip them
74-
using var memoryStream = new MemoryStream();
75-
using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
76-
{
77-
foreach (var item in args.Data)
78-
{
79-
if (!int.TryParse(item.Id, out int id))
80-
continue;
81-
82-
var document = await _documentContext.Documents.FirstOrDefaultAsync(d => d.Id == id);
83-
if (document == null || document.FileData == null)
84-
continue;
85-
86-
var entry = zip.CreateEntry(document.Name, CompressionLevel.Fastest);
87-
using var entryStream = entry.Open();
88-
await entryStream.WriteAsync(document.FileData, 0, document.FileData.Length);
89-
}
90-
}
91-
92-
memoryStream.Seek(0, SeekOrigin.Begin);
93-
return File(memoryStream.ToArray(), "application/zip", "Documents.zip");
94-
}
95-
9644
/// <summary>
9745
/// Returns document data as serialized SFDT JSON for Syncfusion DocumentEditor.
9846
/// </summary>
@@ -165,6 +113,58 @@ public async Task<IActionResult> SaveDocumentAsync(int id, [FromBody] SaveDocume
165113
}
166114
}
167115

116+
/// <summary>
117+
/// Downloads one or more documents. Zips multiple files.
118+
/// </summary>
119+
[HttpPost("downloadAsync")]
120+
public async Task<IActionResult> DownloadAsync([FromForm] string downloadInput)
121+
{
122+
if (string.IsNullOrWhiteSpace(downloadInput))
123+
return BadRequest("Missing download input");
124+
125+
var args = JsonConvert.DeserializeObject<FileManagerDirectoryContent>(downloadInput);
126+
127+
if (args.Data == null || args.Data.Length == 0)
128+
return BadRequest("No files to download.");
129+
130+
// Single file case
131+
if (args.Data.Length == 1)
132+
{
133+
var fileItem = args.Data[0];
134+
if (!int.TryParse(fileItem.Id, out int id))
135+
return BadRequest("Invalid file ID.");
136+
137+
var document = await _documentContext.Documents.FirstOrDefaultAsync(d => d.Id == id);
138+
if (document == null || document.FileData == null || document.FileData.Length == 0)
139+
return NotFound("File not found or is empty.");
140+
141+
var contentType = GetContentType(document.Name);
142+
return File(document.FileData, contentType, document.Name);
143+
}
144+
145+
// Multiple files - zip them
146+
using var memoryStream = new MemoryStream();
147+
using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
148+
{
149+
foreach (var item in args.Data)
150+
{
151+
if (!int.TryParse(item.Id, out int id))
152+
continue;
153+
154+
var document = await _documentContext.Documents.FirstOrDefaultAsync(d => d.Id == id);
155+
if (document == null || document.FileData == null)
156+
continue;
157+
158+
var entry = zip.CreateEntry(document.Name, CompressionLevel.Fastest);
159+
using var entryStream = entry.Open();
160+
await entryStream.WriteAsync(document.FileData, 0, document.FileData.Length);
161+
}
162+
}
163+
164+
memoryStream.Seek(0, SeekOrigin.Begin);
165+
return File(memoryStream.ToArray(), "application/zip", "Documents.zip");
166+
}
167+
168168
/// <summary>
169169
/// Checks if a document with the given name exists in the PostgreSQL database.
170170
/// Expects a JSON payload with a "fileName" property.

0 commit comments

Comments
 (0)