do not work resume download help

Giganews Newsgroups
Subject: do not work resume download help
Posted by:  SW (s_tka…@infonet.by)
Date: Sun, 19 Dec 2004

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPServer, IdCustomHTTPServer,
  IdHTTPServer, Math;

type
  TRangeFileStream = class(TFileStream)
  private
        FRangeStart, FRangeEnd: Cardinal;
        FRangeEnabled: Boolean;
  function GetAbsolutePosition: Longint;
  protected
          property AbsolutePosition: Longint read GetAbsolutePosition;
  public
  constructor Create(const AFileName: String; ARangeStart, ARangeEnd:
Cardinal);
  function Read(var Buffer; Count: Longint): Longint; override;
  function Seek(Offset: Longint; Origin: Word): Longint; override;
  property RangeStart: Cardinal read FRangeStart;
  property RangeEnd: Cardinal read FRangeEnd;
  end;

  TForm1 = class(TForm)
    IdHTTPServer1: TIdHTTPServer;
    procedure IdHTTPServer1CommandGet(AThread: TIdPeerThread;
      ARequestInfo: TIdHTTPRequestInfo;
      AResponseInfo: TIdHTTPResponseInfo);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

constructor TRangeFileStream.Create(const AFileName: String; ARangeStart,
ARangeEnd: Cardinal);
var
  LSize: Longint;
begin
    inherited Create(AFileName, fmOpenRead or fmShareDenyWrite);
    LSize := Self.Size;

    if ARangeEnd=0 then ARangeEnd:=LSize;

    if ARangeStart > LSize then
    begin
        ARangeStart := LSize;
    end;

    if ARangeEnd > LSize then
    begin
        ARangeEnd := LSize;
    end;

    FRangeEnabled := (ARangeStart <= ARangeEnd) and (ARangeEnd <> 0);

    if FRangeEnabled then
    begin
      FRangeStart := ARangeStart;
      FRangeEnd := ARangeEnd;
    end;
end;

function TRangeFileStream.GetAbsolutePosition: Longint;
begin
  Result := inherited Seek(0, soFromCurrent);
end;

function TRangeFileStream.Read(var Buffer; Count: Longint): Longint;
var
  LBytesLeft: Cardinal;
begin
  if FRangeEnabled then
  begin
    LBytesLeft := (FRangeEnd+1) - AbsolutePosition;
    if Count > LBytesLeft then
    begin
      Count := LBytesLeft;
    end;
  end;
  Result := inherited Read(Buffer, Count);
end;

function TRangeFileStream.Seek(Offset: Longint; Origin: Word): Longint;
var
  LPos: Longint;
begin
  if FRangeEnabled then
  begin
    Case Origin of
      soFromBeginning: Offset := FRangeStart + Offset;
      soFromCurrent: Offset := AbsolutePosition + Offset;
      soFromEnd: Offset := (FRangeEnd+1) + Offset;
    end;
    Offset := Max(Offset, FRangeStart);
    Offset := Min(Offset, FRangeEnd+1);
    Origin := soFromBeginning;
  end;
  Result := inherited Seek(Offset, Origin);
end;

{$R *.dfm}

procedure TForm1.IdHTTPServer1CommandGet(AThread: TIdPeerThread;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
    FileStrm: TRangeFileStream;
    LSize, LNumRead: Integer;
    LBuf: array[0..1023] of Byte;
begin
    FileStrm := TRangeFileStream.Create('D:\DIVX\1.avi',
ARequestInfo.ContentRangeStart, ARequestInfo.ContentRangeEnd);
    try
        LSize := FileStrm.Size;

        AResponseInfo.ContentType := 'application/zip';
        AResponseInfo.ContentLength := LSize;
        AResponseInfo.ContentRangeStart := FileStrm.RangeStart;
        AResponseInfo.ContentRangeEnd := FileStrm.RangeEnd;
        AResponseInfo.WriteHeader;

        FileStrm.Position := FileStrm.RangeStart;
        while FileStrm.Position < LSize do
        begin
          LNumRead := FileStrm.Read(LBuf, Sizeof(LBuf));
          if (LNumRead > 0) then
          begin
            AThread.Connection.WriteBuffer(LBuf, LNumRead);
          end;
        end;

    finally
            FileStrm.Free;
    end;

end;

end.

Replies