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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
|
{ lNet MIME Wrapper
CopyRight (C) 2007-2008 by Ales Katona
This library is Free software; you can rediStribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is diStributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; withOut even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
for more details.
You should have received a Copy of the GNU Library General Public License
along with This library; if not, Write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
This license has been modified. See File LICENSE.ADDON for more inFormation.
Should you find these sources without a LICENSE File, please contact
me at ales@chello.sk
}
unit lMimeWrapper;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes, Contnrs, lMimeStreams;
const
MIME_VERSION = 'MIME-version: 1.0' + CRLF;
type
TMimeEncoding = (me8bit, meBase64);
TMimeDisposition = (mdInline, mdAttachment);
{ TMimeSection }
TMimeSection = class
protected
FContentType: string;
FEncoding: TMimeEncoding;
FActivated: Boolean;
FDescription: string;
FDisposition: TMimeDisposition;
FBuffer: string;
FEncodingStream: TStream;
FOutputStream: TStream;
FLocalStream: TBogusStream;
function RecalculateSize(const OriginalSize: Integer): Integer;
function GetSize: Integer; virtual; abstract;
procedure SetDescription(const AValue: string);
procedure SetDisposition(const AValue: TMimeDisposition);
procedure SetEncoding(const AValue: TMimeEncoding);
procedure CreateEncodingStream; virtual;
function GetHeader: string; virtual;
function ReadBuffer(const aSize: Integer): string;
procedure FillBuffer(const aSize: Integer); virtual; abstract;
public
constructor Create(aOutputStream: TStream);
destructor Destroy; override;
function Read(const aSize: Integer): Integer;
procedure Reset; virtual;
public
property ContentType: string read FContentType write FContentType;
property Encoding: TMimeEncoding read FEncoding write SetEncoding;
property Disposition: TMimeDisposition read FDisposition write SetDisposition;
property Description: string read FDescription write SetDescription;
property Header: string read GetHeader;
property Size: Integer read GetSize;
end;
{ TMimeTextSection }
TMimeTextSection = class(TMimeSection)
protected
FOriginalData: string;
FData: string;
function GetSize: Integer; override;
procedure SetData(const AValue: string);
function GetCharset: string;
procedure SetCharset(const AValue: string);
procedure FillBuffer(const aSize: Integer); override;
public
constructor Create(aOutputStream: TStream; const aText: string);
procedure Reset; override;
public
property Charset: string read GetCharset write SetCharset;
property Text: string read FData write SetData;
end;
{ TMimeStreamSection }
TMimeStreamSection = class(TMimeSection)
protected
FStream: TStream;
FOwnsStreams: Boolean;
FOriginalPosition: Int64;
function GetSize: Integer; override;
procedure SetStream(aValue: TStream);
procedure FillBuffer(const aSize: Integer); override;
public
constructor Create(aOutputStream: TStream; aStream: TStream);
destructor Destroy; override;
procedure Reset; override;
public
property Stream: TStream read FStream write SetStream;
property OwnsStreams: Boolean read FOwnsStreams write FOwnsStreams;
end;
{ TMimeFileSection }
TMimeFileSection = class(TMimeStreamSection)
protected
FFileName: string;
procedure SetFileName(const AValue: string);
procedure SetContentType(const aFileName: string);
function GetHeader: string; override;
public
constructor Create(aOutputStream: TStream; const aFileName: string);
property FileName: string read FFileName write SetFileName;
end;
{ TMimeStream }
TMimeStream = class(TStream)
protected
FSections: TFPObjectList;
FOutputStream: TMimeOutputStream;
FBoundary: string;
FActiveSection: Integer;
FCalledRead: Boolean;
FCalledWrite: Boolean;
function GetBoundarySize: Integer;
function GetSize: Int64; override;
function GetCount: Integer;
function GetBoundary: string;
function GetSection(i: Integer): TMimeSection;
function GetMimeHeader: string;
procedure SetSection(i: Integer; const AValue: TMimeSection);
procedure ActivateFirstSection;
procedure ActivateNextSection;
procedure DoRead(const aSize: Integer);
public
constructor Create;
destructor Destroy; override;
function Seek(Offset: Longint; Origin: Word): Longint; overload; override;
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; overload; override;
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
procedure AddTextSection(const aText: string; const aCharSet: string = 'UTF-8');
procedure AddFileSection(const aFileName: string);
procedure AddStreamSection(aStream: TStream; const FreeStream: Boolean = False);
procedure Delete(const i: Integer);
procedure Remove(aSection: TMimeSection);
procedure Reset;
public
property Sections[i: Integer]: TMimeSection read GetSection write SetSection; default;
property Count: Integer read GetCount;
property Boundary: string read FBoundary;
end;
{ EAlreadyActivatedException }
EAlreadyActivatedException = class(Exception)
public
constructor Create;
end;
{ EAlreadyCalledReadException }
EAlreadyCalledReadException = class(Exception)
public
constructor Create;
end;
{ EAlreadyCalledWriteException }
EAlreadyCalledWriteException = class(Exception)
public
constructor Create;
end;
implementation
uses
Math, Base64;
function EncodingToStr(const Encoding: TMimeEncoding): string;
begin
Result := '';
case Encoding of
me8bit : Result := '8bit';
meBase64 : Result := 'base64';
end;
end;
function DispositionToStr(const Disposition: TMimeDisposition): string;
begin
Result := '';
case Disposition of
mdInline : Result := 'inline';
mdAttachment : Result := 'attachment';
end;
end;
{ TMimeSection }
function TMimeSection.RecalculateSize(const OriginalSize: Integer): Integer;
begin
Result := 0;
if OriginalSize = 0 then
Exit;
case FEncoding of
me8bit : Result := OriginalSize;
meBase64 : if OriginalSize mod 3 = 0 then
Result := (OriginalSize div 3) * 4 // this is simple, 4 bytes per 3 bytes
else
Result := ((OriginalSize + 3) div 3) * 4; // add "padding" trupplet
end;
end;
procedure TMimeSection.SetDescription(const AValue: string);
begin
if not FActivated then
FDescription := AValue;
end;
procedure TMimeSection.SetDisposition(const AValue: TMimeDisposition);
begin
if not FActivated then
FDisposition := AValue;
end;
procedure TMimeSection.SetEncoding(const AValue: TMimeEncoding);
begin
if not FActivated then begin
FEncoding := aValue;
if Assigned(FEncodingStream) then
FEncodingStream.Free;
CreateEncodingStream;
end;
end;
procedure TMimeSection.CreateEncodingStream;
begin
case FEncoding of
me8bit : FEncodingStream := nil;
meBase64 : FEncodingStream := TBase64EncodingStream.Create(FLocalStream);
end;
end;
function TMimeSection.GetHeader: string;
begin
Result := 'Content-Type: ' + FContentType + CRLF;
Result := Result + 'Content-Transfer-Encoding: ' + EncodingToStr(FEncoding) + CRLF;
Result := Result + 'Content-Disposition: ' + DispositionToStr(FDisposition) + CRLF;
if Length(FDescription) > 0 then
Result := Result + 'Content-Description: ' + FDescription + CRLF;
Result := Result + CRLF;
end;
function TMimeSection.ReadBuffer(const aSize: Integer): string;
begin
Result := '';
if aSize >= Length(FBuffer) then
FillBuffer(aSize);
Result := Copy(FBuffer, 1, aSize);
end;
constructor TMimeSection.Create(aOutputStream: TStream);
begin
FOutputStream := aOutputStream;
FEncodingStream := nil;
FLocalStream := TBogusStream.Create;
end;
destructor TMimeSection.Destroy;
begin
if Assigned(FEncodingStream) then
FEncodingStream.Free;
FLocalStream.Free;
inherited Destroy;
end;
function TMimeSection.Read(const aSize: Integer): Integer;
var
s: string;
begin
Result := 0;
if aSize <= 0 then
Exit;
if not FActivated then begin
FActivated := True;
FBuffer := GetHeader;
end;
if Length(FBuffer) < aSize then
FillBuffer(aSize);
s := ReadBuffer(aSize);
if Length(s) >= aSize then begin
Result := FOutputStream.Write(s[1], aSize);
Delete(FBuffer, 1, Result);
end else if Length(s) > 0 then begin
Result := FOutputStream.Write(s[1], Length(s));
Delete(FBuffer, 1, Result);
end;
end;
procedure TMimeSection.Reset;
begin
FActivated := False;
FBuffer := '';
FLocalStream.Reset;
SetEncoding(FEncoding);
end;
{ TMimeTextSection }
procedure TMimeTextSection.SetCharset(const aValue: string);
begin
if not FActivated then begin
if Length(aValue) > 0 then
FContentType := 'text/plain; charset="' + aValue + '"'
else
FContentType := 'text/plain';
end;
end;
procedure TMimeTextSection.FillBuffer(const aSize: Integer);
var
s: string;
n: Integer;
begin
s := Copy(FData, 1, aSize);
if Length(s) = 0 then
Exit;
n := aSize;
if Assigned(FEncodingStream) then begin
n := FEncodingStream.Write(s[1], Length(s));
Delete(FData, 1, n);
if Length(FData) = 0 then begin
FEncodingStream.Free; // to fill in the last bit
CreateEncodingStream;
FLocalStream.Write(CRLF[1], Length(CRLF));
end;
SetLength(s, FLocalStream.Size);
SetLength(s, FLocalStream.Read(s[1], Length(s)));
end else begin
Delete(FData, 1, n);
if Length(FData) = 0 then
s := s + CRLF;
end;
FBuffer := FBuffer + s;
end;
function TMimeTextSection.GetSize: Integer;
begin
if FActivated then
Result := Length(FBuffer) + RecalculateSize(Length(FData))
else
Result := Length(FBuffer) + Length(GetHeader) + RecalculateSize(Length(FData));
if not FActivated
or (Length(FBuffer) > 0)
or (Length(FData) > 0) then
if Length(FOriginalData) > 0 then
Result := Result + Length(CRLF); // CRLF after each msg body
end;
procedure TMimeTextSection.SetData(const AValue: string);
begin
if not FActivated then begin
FOriginalData := aValue;
FData := aValue;
end;
end;
function TMimeTextSection.GetCharset: string;
var
n: Integer;
begin
Result := '';
n := Pos('=', FContentType);
if n > 0 then
Result := StringReplace(Copy(FContentType, n + 1, Length(FContentType)),
'"', '', [rfReplaceAll]);
end;
constructor TMimeTextSection.Create(aOutputStream: TStream; const aText: string);
begin
inherited Create(aOutputStream);
FContentType := 'text/plain; charset="UTF-8"';
FOriginalData := aText;
FData := FOriginalData;
end;
procedure TMimeTextSection.Reset;
begin
inherited Reset;
FData := FOriginalData;
end;
{ TMimeStreamSection }
function TMimeStreamSection.GetSize: Integer;
begin
if FActivated then
Result := Length(FBuffer) + RecalculateSize(FStream.Size - FStream.Position)
else
Result := Length(FBuffer) + Length(GetHeader) + RecalculateSize(FStream.Size - FStream.Position);
if not FActivated
or (Length(FBuffer) > 0)
or (FStream.Size - FStream.Position > 0) then
if FStream.Size - FOriginalPosition > 0 then
Result := Result + Length(CRLF); // CRLF after each msg body
end;
procedure TMimeStreamSection.SetStream(aValue: TStream);
begin
if Assigned(FStream)
and FOwnsStreams then begin
FStream.Free;
FStream := nil;
end;
FStream := aValue;
FOriginalPosition := FStream.Position;
end;
procedure TMimeStreamSection.FillBuffer(const aSize: Integer);
var
s: string;
n: Integer;
begin
SetLength(s, aSize);
SetLength(s, FStream.Read(s[1], aSize));
if Length(s) <= 0 then
Exit;
if Assigned(FEncodingStream) then begin
n := FEncodingStream.Write(s[1], Length(s));
if n < Length(s) then
FStream.Position := FStream.Position - (n - Length(s));
if FStream.Size - FStream.Position = 0 then begin
FEncodingStream.Free; // to fill in the last bit
CreateEncodingStream;
FLocalStream.Write(CRLF[1], Length(CRLF));
end;
SetLength(s, FLocalStream.Size);
SetLength(s, FLocalStream.Read(s[1], FLocalStream.Size));
end else if FStream.Size - FStream.Position = 0 then
s := s + CRLF;
FBuffer := FBuffer + s;
end;
constructor TMimeStreamSection.Create(aOutputStream: TStream; aStream: TStream);
begin
inherited Create(aOutputStream);
FDisposition := mdAttachment;
FStream := aStream;
FOriginalPosition := FStream.Position;
FContentType := 'application/octet-stream';
end;
destructor TMimeStreamSection.Destroy;
begin
if FOwnsStreams then
FStream.Free;
inherited Destroy;
end;
procedure TMimeStreamSection.Reset;
begin
inherited Reset;
FStream.Position := FOriginalPosition;
end;
{ TMimeStream }
function TMimeStream.GetBoundarySize: Integer;
var
n: Integer;
begin
Result := 0;
if FSections.Count > 1 then begin
n := Max(FActiveSection, 0);
Result := (Length(FBoundary) + 4) * (FSections.Count - n) + 2;
// # sections * (boundarylength + --CRLF +) ending --
end;
end;
function TMimeStream.GetSize: Int64;
var
i: Integer;
begin
Result := 0;
if FActiveSection > -2 then
for i := 0 to Count - 1 do
Result := Result + TMimeSection(FSections[i]).Size;
if FActiveSection = -1 then // not yet active, must add header info
Result := Result + Length(GetMimeHeader) + GetBoundarySize;
Result := Result + FOutputStream.Size;
end;
function TMimeStream.GetCount: Integer;
begin
Result := FSections.Count;
end;
function TMimeStream.GetBoundary: string;
var
i: Integer;
begin
Result := '';
for i := 1 to 25 + Random(15) do
Result := Result + Char(Random(Ord('9') - Ord('0') + 1) + Ord('0'));
end;
function TMimeStream.GetSection(i: Integer): TMimeSection;
begin
Result := nil;
if (i >= 0)
and (i < FSections.Count) then
Result := TMimeSection(FSections[i]);
end;
function TMimeStream.GetMimeHeader: string;
const
MIME_HEADER = 'Content-type: multipart/mixed; boundary="';
begin
Result := MIME_VERSION;
if FSections.Count > 1 then
Result := Result + MIME_HEADER + FBoundary + '"' + CRLF + CRLF +
'This is a multi-part message in MIME format.' + CRLF +
'--' + FBoundary + CRLF;
end;
procedure TMimeStream.SetSection(i: Integer; const AValue: TMimeSection);
begin
if (i >= 0)
and (i < FSections.Count) then
FSections[i] := aValue;
end;
procedure TMimeStream.ActivateFirstSection;
var
s: string;
begin
if FActiveSection = -1 then
if FSections.Count > 0 then begin
FActiveSection := 0;
s := GetMimeHeader;
FOutputStream.Write(s[1], Length(s));
end;
end;
procedure TMimeStream.ActivateNextSection;
var
s: string;
begin
Inc(FActiveSection);
if FSections.Count > 1 then begin
if FActiveSection >= FSections.Count then
s := '--' + FBoundary + '--' + CRLF
else
s := '--' + FBoundary + CRLF;
FOutputStream.Write(s[1], Length(s));
end;
if FActiveSection >= FSections.Count then
FActiveSection := -2;
end;
procedure TMimeStream.DoRead(const aSize: Integer);
begin
ActivateFirstSection;
if FActiveSection < 0 then
Exit;
TMimeSection(FSections[FActiveSection]).Read(aSize);
if TMimeSection(FSections[FActiveSection]).Size = 0 then
ActivateNextSection;
end;
constructor TMimeStream.Create;
begin
Randomize;
FActiveSection := -1;
FBoundary := GetBoundary;
FSections := TFPObjectList.Create(True);
FOutputStream := TMimeOutputStream.Create(@DoRead);
end;
destructor TMimeStream.Destroy;
begin
FSections.Free;
FOutputStream.Free;
inherited Destroy;
end;
function TMimeStream.Seek(Offset: Longint; Origin: Word): Longint;
begin
Result := Offset;
end;
function TMimeStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
Result := Offset;
end;
function TMimeStream.Read(var Buffer; Count: Longint): Longint;
begin
if Count <= 0 then
Exit(0);
if FCalledWrite then
raise EAlreadyCalledWriteException.Create;
FCalledRead := True;
Result := FOutputStream.Read(Buffer, Count);
end;
function TMimeStream.Write(const Buffer; Count: Longint): Longint;
begin
if Count <= 0 then
Exit(0);
if FCalledRead then
raise EAlreadyCalledReadException.Create;
Result := 0;
FCalledWrite := True;
raise Exception.Create('Not yet implemented');
end;
procedure TMimeStream.AddTextSection(const aText: string; const aCharSet: string = 'UTF-8');
var
s: TMimeTextSection;
begin
if FActiveSection >= 0 then
raise EAlreadyActivatedException.Create;
s := TMimeTextSection.Create(FOutputStream, aText);
s.Charset := aCharSet;
FSections.Add(s);
end;
procedure TMimeStream.AddFileSection(const aFileName: string);
begin
if FActiveSection >= 0 then
raise EAlreadyActivatedException.Create;
FSections.Add(TMimeFileSection.Create(FOutputStream, aFileName));
end;
procedure TMimeStream.AddStreamSection(aStream: TStream; const FreeStream: Boolean
);
var
s: TMimeStreamSection;
begin
if FActiveSection >= 0 then
raise EAlreadyActivatedException.Create;
s := TMimeStreamSection.Create(FOutputStream, aStream);
if FreeStream then
s.OwnsStreams := True;
FSections.Add(s);
end;
procedure TMimeStream.Delete(const i: Integer);
begin
if (i >= 0) and (i < Count) then
FSections.Delete(i);
end;
procedure TMimeStream.Remove(aSection: TMimeSection);
begin
FSections.Remove(aSection);
end;
procedure TMimeStream.Reset;
var
i: Integer;
begin
FCalledRead := False;
FCalledWrite := False;
for i := 0 to FSections.Count - 1 do
TMimeSection(FSections[i]).Reset;
FOutputStream.Reset;
FActiveSection := -1;
end;
{ TMimeFileSection }
procedure TMimeFileSection.SetFileName(const AValue: string);
begin
if not FActivated then begin
FFileName := aValue;
Stream := TFileStream.Create(aValue, fmOpenRead);
SetContentType(aValue);
end;
end;
procedure TMimeFileSection.SetContentType(const aFileName: string);
var
s: string;
begin
s := StringReplace(ExtractFileExt(aFileName), '.', '', [rfReplaceAll]);
if (s = 'txt')
or (s = 'pas')
or (s = 'pp')
or (s = 'pl')
or (s = 'cpp')
or (s = 'cc')
or (s = 'h')
or (s = 'hh')
or (s = 'rb')
or (s = 'pod')
or (s = 'php')
or (s = 'php3')
or (s = 'php4')
or (s = 'php5')
or (s = 'c++') then FContentType := 'text/plain';
if (s = 'html')
or (s = 'shtml') then FContentType := 'text/html';
if s = 'css' then FContentType := 'text/css';
if s = 'png' then FContentType := 'image/x-png';
if s = 'xpm' then FContentType := 'image/x-pixmap';
if s = 'xbm' then FContentType := 'image/x-bitmap';
if (s = 'tif')
or (s = 'tiff') then FContentType := 'image/tiff';
if s = 'mng' then FContentType := 'image/x-mng';
if s = 'gif' then FContentType := 'image/gif';
if s = 'rgb' then FContentType := 'image/rgb';
if (s = 'jpg')
or (s = 'jpeg') then FContentType := 'image/jpeg';
if s = 'bmp' then FContentType := 'image/x-ms-bmp';
if s = 'wav' then FContentType := 'audio/x-wav';
if s = 'mp3' then FContentType := 'audio/x-mp3';
if s = 'ogg' then FContentType := 'audio/x-ogg';
if s = 'avi' then FContentType := 'video/x-msvideo';
if (s = 'qt')
or (s = 'mov') then FContentType := 'video/quicktime';
if (s = 'mpg')
or (s = 'mpeg') then FContentType := 'video/mpeg';
if s = 'pdf' then FContentType := 'application/pdf';
if s = 'rtf' then FContentType := 'application/rtf';
if s = 'tex' then FContentType := 'application/x-tex';
if s = 'latex' then FContentType := 'application/x-latex';
if s = 'doc' then FContentType := 'application/msword';
if s = 'gz' then FContentType := 'application/x-gzip';
if s = 'zip' then FContentType := 'application/zip';
if s = '7z' then FContentType := 'application/x-7zip';
if s = 'rar' then FContentType := 'application/rar';
if s = 'tar' then FContentType := 'application/x-tar';
if s = 'arj' then FContentType := 'application/arj';
end;
function TMimeFileSection.GetHeader: string;
begin
Result := 'Content-Type: ' + FContentType + CRLF;
Result := Result + 'Content-Transfer-Encoding: ' + EncodingToStr(FEncoding) + CRLF;
Result := Result + 'Content-Disposition: ' + DispositionToStr(FDisposition) +
'; filename="' + FFileName + '"' + CRLF;
if Length(FDescription) > 0 then
Result := Result + 'Content-Description: ' + FDescription + CRLF;
Result := Result + CRLF;
end;
constructor TMimeFileSection.Create(aOutputStream: TStream; const aFileName: string);
begin
inherited Create(aOutputStream, TFileStream.Create(aFileName, fmOpenRead));
SetContentType(aFileName);
FDescription := ExtractFileName(aFileName);
Encoding := meBase64;
FFileName := ExtractFileName(aFileName);
FOwnsStreams := True;
end;
{ EAlreadyActivatedException }
constructor EAlreadyActivatedException.Create;
begin
inherited Create('The stream or section has already been activated (by Read() or Write())');
end;
{ EAlreadyCalledReadException }
constructor EAlreadyCalledReadException.Create;
begin
inherited Create('The stream has already been used for reading');
end;
{ EAlreadyCalledWriteException }
constructor EAlreadyCalledWriteException.Create;
begin
inherited Create('The stream has already been used for writing');
end;
end.
|