Skip to content

Commit c6b965f

Browse files
committed
feat: add pdf viewer
1 parent 41e81af commit c6b965f

4 files changed

Lines changed: 573 additions & 1 deletion

File tree

TelegramDownloader/Controllers/FileController.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,50 @@ public async Task<IActionResult> GetFile(string id, string? idChannel, string? i
270270
{
271271
FileDownloadName = fileName,
272272
EnableRangeProcessing = true
273-
273+
274+
};
275+
}
276+
277+
/// <summary>
278+
/// View file inline (for PDF viewer, etc.) - doesn't trigger download
279+
/// </summary>
280+
[Route("ViewFile/{id}")]
281+
public async Task<IActionResult> ViewFile(string id, string? idChannel, string? idFile)
282+
{
283+
var fileName = id;
284+
var mimeType = FileService.getMimeType(id.Split(".").Last());
285+
var file = _fs.ExistFileIntempFolder($"{idChannel}-{idFile}-{id}");
286+
if (file == null)
287+
{
288+
TL.Message idM = await _ts.getMessageFile(idChannel, Convert.ToInt32(idFile));
289+
ChatMessages cm = new ChatMessages();
290+
cm.message = idM;
291+
string filePath = System.IO.Path.Combine(FileService.TEMPDIR, "_temp", $"{idChannel}-{idFile}-{id}");
292+
file = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
293+
DownloadModel dm = new DownloadModel();
294+
dm.tis = _tis;
295+
dm.startDate = DateTime.Now;
296+
dm.path = filePath;
297+
if (cm.message is Message msgBase)
298+
{
299+
if (msgBase.media is MessageMediaDocument mediaDoc &&
300+
mediaDoc.document is TL.Document doc)
301+
{
302+
dm._size = doc.size;
303+
dm.name = doc.Filename;
304+
}
305+
}
306+
dm.channelName = _ts.getChatName(Convert.ToInt64(idChannel));
307+
_tis.addToDownloadList(dm);
308+
await _ts.DownloadFileAndReturn(cm, file, model: dm);
309+
file.Position = 0;
310+
}
311+
312+
// Return file for inline viewing (no download header)
313+
Response.Headers["Content-Disposition"] = $"inline; filename=\"{HttpUtility.UrlEncode(fileName)}\"";
314+
return new FileStreamResult(file, mimeType)
315+
{
316+
EnableRangeProcessing = true
274317
};
275318
}
276319

0 commit comments

Comments
 (0)