@@ -267,6 +267,36 @@ public async Task<IActionResult> GetFile(string id, string? idChannel, string? i
267267 } ;
268268 }
269269
270+ [ Route ( "GetFileByTfmId/{id}" ) ]
271+ public async Task < IActionResult > GetFileByTfmId ( string id , [ FromQuery ] string idChannel , [ FromQuery ] string idFile )
272+ {
273+ var fileName = id ;
274+ var mimeType = FileService . getMimeType ( id . Split ( "." ) . Last ( ) ) ;
275+ var dbFile = await _fs . getItemById ( idChannel , idFile ) ;
276+ if ( dbFile == null )
277+ {
278+ return new ObjectResult ( "" ) { StatusCode = ( int ) HttpStatusCode . NotFound } ;
279+ }
280+ var file = _fs . ExistFileIntempFolder ( $ "{ idChannel } -{ dbFile . MessageId } -{ id } ") ;
281+ if ( file == null )
282+ {
283+ TL . Message idM = await _ts . getMessageFile ( idChannel , Convert . ToInt32 ( dbFile . MessageId ) ) ;
284+ ChatMessages cm = new ChatMessages ( ) ;
285+ cm . message = idM ;
286+ file = new FileStream ( System . IO . Path . Combine ( FileService . TEMPDIR , "_temp" , $ "{ idChannel } -{ idFile } -{ id } ") , FileMode . Create , FileAccess . ReadWrite ) ;
287+ DownloadModel dm = new DownloadModel ( ) ;
288+ dm . tis = _tis ;
289+ dm . channelName = _ts . getChatName ( Convert . ToInt64 ( idChannel ) ) ;
290+ _tis . addToDownloadList ( dm ) ;
291+ await _ts . DownloadFileAndReturn ( cm , file , model : dm ) ;
292+ file . Position = 0 ;
293+ }
294+
295+ Response . Headers [ "Content-Disposition" ] = $ "inline; filename=\" { HttpUtility . UrlEncode ( fileName ) } \" ";
296+
297+ return new FileStreamResult ( file , mimeType ) ;
298+ }
299+
270300 [ Route ( "strm" ) ]
271301 public async Task < IActionResult > GetStrm ( [ FromQuery ] string idChannel , [ FromQuery ] string path , [ FromQuery ] string host )
272302 {
@@ -424,109 +454,6 @@ public async Task<IActionResult> GetFileStream(string idChannel, string idFile,
424454
425455 }
426456
427- [ Route ( "GetFileStream2/{idChannel}/{idFile}/{name}" ) ]
428- public async Task < IActionResult > GetFileStream2 ( string idChannel , string idFile , string name )
429- {
430- var fileName = name ;
431- var mimeType = FileService . getMimeType ( name . Split ( "." ) . Last ( ) ) ;
432-
433- var request = HttpContext . Request ;
434- var rangeHeader = request . Headers [ "Range" ] . ToString ( ) ;
435-
436- var file = await _fs . getItemById ( idChannel , idFile ) ;
437-
438- // HttpResponseMessage fullResponse = new HttpResponseMessage(HttpStatusCode.OK); // Request.CreateResponse(HttpStatusCode.OK);
439- TL . Message idM = await _ts . getMessageFile ( idChannel , file . MessageId ?? file . ListMessageId . FirstOrDefault ( ) ) ;
440- //byte[] data = await _ts.DownloadFileStream(idM, 0, 512);
441- //var stream = new MemoryStream(data);
442-
443- // Supón que puedes obtener el tamaño total del archivo remoto
444- long totalLength = ( await _fs . getItemById ( idChannel , idFile ) ) . Size ;
445-
446- long from = 0 ;
447- long initialFrom = 0 ;
448- long to = 0 ;
449-
450-
451-
452- if ( ! string . IsNullOrEmpty ( rangeHeader ) && rangeHeader . StartsWith ( "bytes=" ) )
453- {
454- var range = rangeHeader . Replace ( "bytes=" , "" ) . Split ( '-' ) ;
455- from = long . Parse ( range [ 0 ] ) ;
456- initialFrom = from ;
457- if ( range . Length > 1 && ! string . IsNullOrEmpty ( range [ 1 ] ) )
458- to = long . Parse ( range [ 1 ] ) ;
459- }
460-
461- if ( from > 0 )
462- {
463- from = ( from / 524288 ) * 524288 ;
464- }
465-
466- if ( to == 0 )
467- to = ( 25 * 524288 ) + from ;
468- else
469- to = ( ( to + 524288 ) / 524288 ) * 524288 ;
470-
471- if ( to > totalLength )
472- {
473- to = totalLength - 1 ;
474- }
475-
476- long length = to - from ;
477-
478- byte [ ] data = await _ts . DownloadFileStream ( idM , from , ( int ) length ) ;
479-
480- long skipedBytes = initialFrom - from ; // (data.Length + initialFrom) >= totalLength ? data.Length - (totalLength - initialFrom) : initialFrom - from;
481-
482- if ( skipedBytes < 0 ) skipedBytes = 0 ;
483-
484- if ( skipedBytes >= data . Length )
485- return StatusCode ( 500 , "No hay suficientes bytes en el bloque descargado" ) ;
486-
487- long dataLength = data . Length - skipedBytes ;
488- Response . ContentLength = ( dataLength + initialFrom ) >= totalLength ? totalLength - initialFrom : dataLength ;
489- // Response.StatusCode = (int)HttpStatusCode.PartialContent; // 206
490- Response . StatusCode = StatusCodes . Status206PartialContent ;
491- Response . ContentType = mimeType ;
492- Response . Headers . Add ( "Content-Range" , $ "bytes { initialFrom } -{ ( initialFrom + Response . ContentLength - 1 ) } /{ totalLength } ") ;
493- Response . Headers . Add ( "Accept-Ranges" , "bytes" ) ;
494-
495- if ( Request . Headers . ContainsKey ( "Range" ) )
496- {
497- Console . WriteLine ( "Range header recibido:" ) ;
498- Console . WriteLine ( Request . Headers [ "Range" ] ) ;
499- }
500-
501- foreach ( var header in Response . Headers )
502- {
503- Console . WriteLine ( $ "{ header . Key } : { header . Value } ") ;
504- }
505-
506-
507-
508-
509- var stream = new MemoryStream ( data , ( int ) skipedBytes , ( int ) Response . ContentLength ) ;
510-
511- Console . WriteLine ( "Real Data length: " + stream . Length ) ;
512-
513- Console . WriteLine ( "---------------------------------------------------" ) ;
514-
515- //return new FileStreamResult(stream, mimeType);
516- var cancellationToken = HttpContext . RequestAborted ;
517-
518- await stream . CopyToAsync ( Response . Body , 64 * 1024 , cancellationToken ) ;
519- await Response . Body . FlushAsync ( cancellationToken ) ;
520-
521- return new EmptyResult ( ) ;
522- // return File(stream, mimeType, enableRangeProcessing: false);
523- //return new FileStreamResult(stream, mimeType)
524- //{
525- // EnableRangeProcessing = false
526- //};
527-
528- }
529-
530457 [ Route ( "export/{id}" ) ]
531458 public async Task < IActionResult > ExportDatabase ( string id )
532459 {
0 commit comments