Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   MIME Emails Download von Bildern - unbekannter Dateityp (https://www.delphipraxis.net/203962-mime-emails-download-von-bildern-unbekannter-dateityp.html)

josef-b 10. Apr 2020 10:48

MIME Emails Download von Bildern - unbekannter Dateityp
 
Ich möchte in Emails eingebettete Bilder im html in ein TrichView einlesen..

dazu habe ich z.B. folgende URL

https://newsvonirgendeinem.com/art_r...=1g76j.21tp18n

Hieraus sieht man ja nicht, was es für eine Datei ist..

ich lade die wie folgt aus dem web und speichere sie als Stream und dann als Datei zwischen...

Delphi-Quellcode:
IDHttp2.Request.Useragent :=
        'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MAAU)';
IdHTTP2.Get(Location, Stream);
Wenn ich jetzt die zwischengespeicherte Datei (mit einem TEMP-Dateinamen, aber der ursprünglichen
Extension in ein Tpicture laden will, gibts natürlich Probleme -> unbekannte Bilddateierweiterung.

DELPHI]
pic := TPicture.Create;
pic.LoadFromFile(FileName);
[/DELPHI]


Bekomme ich den DateiTyp evtl. über die Antwort bei TIDHTTP ? oder sogar den eigentlichen Dateinamen?

Bernhard Geyer 10. Apr 2020 11:09

AW: MIME Emails Download von Bildern - unbekannter Dateityp
 
Du bekommst den Dateityp sicherlich über die ersten Bytes in der Datei raus.
Hier hat jeder Datentyp (relevant wird hier ja nur jpg, png und gif sein) seine sogenannten Magic Bytes.

himitsu 10. Apr 2020 11:33

AW: MIME Emails Download von Bildern - unbekannter Dateityp
 
Ich bin mir nicht sicher seit wann.
Früher hatte Picture.LoadFromFile nur anhand der Dateiendung die Typen unterschieden.
Aktuell ist das bereits im LoadFromStream drin, wo anhand des MagicBytes unterschieden wird, welches dann eine der registrieren TGraphic auswählt.
(wichtig, dafür müssen diese Units auch im Programm irgendwo eingebungen sein, also vorallem für JPEG und PNG)

Kann sein, dass es erst seit 10.1/10.2 so drin ist.

mjustin 10. Apr 2020 11:41

AW: MIME Emails Download von Bildern - unbekannter Dateityp
 
Bei HTTP kann man den Dateityp aus dem Header "Content-Type" auslesen (auch der Browser benötigt den ja um die Daten korrekt anzuzeigen).
In Indy steht dieser Header in der IdHTTP.Response.ContentType Property. Darin stehen Werte die als "Media Types" standardisiert sind, z.B.

image/gif
image/jpeg
image/png
image/tiff

Zitat:

Bekomme ich den DateiTyp evtl. über die Antwort bei TIDHTTP ? oder sogar den eigentlichen Dateinamen?
Der "eigentliche" Dateiname der Datei ist unsichtbar für den Client. Aber es kann im Response Header ein Dateiname vorgeschlagen werden, dieser steht dann im Content-Disposition Header. Beispiel:

Zitat:

Content-Disposition: attachment; filename="filename.jpg"
Allerdings ist es nicht garantiert oder spezifiziert dass dieser Header immer den richtigen Dateityp enthält.

Referenzen:
https://developer.mozilla.org/en-US/...s/Content-Type
https://developer.mozilla.org/en-US/...nt-Disposition

himitsu 10. Apr 2020 11:52

AW: MIME Emails Download von Bildern - unbekannter Dateityp
 
Wobei diese Angabe nicht immer stimmen muß.

Es kommt da drauf an, wie der Webserver das unterscheidet (Dateiendung, oder z.B. auch anhand der MagicBytes) und ob er den Content-Type überhaupt ausgibt.
Aber ja, oft kann man blind davon ausgehn, dass er es macht.

PS: Auch Firefox nimmt diese Angabe nicht blind entgegen, sondern prüft es meistens auch nochmal. :stupid:

Redeemer 10. Apr 2020 14:58

AW: MIME Emails Download von Bildern - unbekannter Dateityp
 
Aus einem meiner Programme kopiert (Code ist 9 Jahre alt nicht steinigen bitte): (Dateiformate löschen, die du nichts brauchst. Target ist i.d.R. TPicture.)
Delphi-Quellcode:
type TImageFormat = (unknown, BMP, GIF, JPG, PNG, PBMa, PGMa, PPMa, PBMb, PGMb, PPMb, XBM, XPM, SVG);

function GetImageFormat(Stream: TStream): TImageFormat;
function GetFormatExt(const ImageFormat: TImageFormat): string;
function LoadImageFromStream(Target: TPersistent; Stream: TStream): Boolean;

function GetImageFormat(Stream: TStream): TImageFormat;
var
  Memory: array[0..8] of Byte;
begin
  Result := unknown;

  try
    if Stream.Size < 10 then
    Exit;

    Stream.Position := 0;
    Stream.Read(Memory, 9);
    case Memory[0] of
      80: begin // P: Netpbm
            case Memory[1] of
              49: Result := PBMa; // 1: Bitmap ASCII
              50: Result := PGMa; // 2: Graymap ASCII
              51: Result := PPMa; // 3: Pixmap ASCII
              52: Result := PBMb; // 4: Bitmap binary
              53: Result := PGMb; // 5: Graymap binary
              54: Result := PPMb; // 6: Pixmap binary
            end;
          end;
      66: if Memory[1] = 77 then // BM: Bitmap
          Result := BMP;
      35: if Memory[1] = 100 then // #define: XBM
          if Memory[2] = 101 then
          if Memory[3] = 102 then
          if Memory[4] = 105 then
          if Memory[5] = 110 then
          if Memory[6] = 101 then
          Result := XBM;
      {33: if Memory[1] = 32 then // ! XPM2: XPM
          if Memory[2] = 88 then
          if Memory[3] = 80 then
          if Memory[4] = 77 then
          if Memory[5] = 50 then
          Result := XPM;            }
      47: if Memory[1] = 42 then // /* XPM */: XPM   /*
          if Memory[2] = 32 then
          if Memory[3] = 88 then
          if Memory[4] = 80 then
          if Memory[5] = 77 then
          if Memory[6] = 32 then
          if Memory[7] = 42 then
          if Memory[8] = 47 then
          Result := XPM;
      137: if Memory[1] = 80 then // &#8240;PNG: PNG
           if Memory[2] = 78 then
           if Memory[3] = 71 then
           if Memory[4] = 13 then
           if Memory[5] = 10 then
           if Memory[6] = 26 then
           if Memory[7] = 10 then
           Result := PNG;
      71: if Memory[1] = 73 then // GIF8...a
          if Memory[2] = 70 then
          if Memory[3] = 56 then
          if Memory[5] = 97 then
          case Memory[4] of
            55: Result := GIF; // 7: GIF, old
            57: Result := GIF; // 9: GIF, new
          end;
      239: if Memory[1] = 187 then // SVG, with BOM
           if Memory[2] = 191 then
           if Memory[3] = 60 then
           Result := SVG;
      60: Result := SVG; // SVG, without BOM
      255: if Memory[1] = 216 then // ÿØ: JPEG
           Result := JPG;
      //47:
    end;
  except
  end;
  Stream.Position := 0;
end;

function GetFormatExt(const ImageFormat: TImageFormat): string;
begin
  case ImageFormat of
    BMP: Result := 'BMP';
    GIF: Result := 'GIF';
    JPG: Result := 'JPG';
    PNG: Result := 'PNG';
    PBMa, PBMb: Result := 'PBM'; // don't care for ASCII/binary if netpbm
    PGMa, PGMb: Result := 'PGM';
    PPMa, PPMb: Result := 'PPM';
    XBM: Result := 'XBM';
    XPM: Result := 'XPM';
    SVG: Result := 'SVG';
    else Result := '';
  end;
end;

function LoadImageFromStream(Target: TPersistent; Stream: TStream): Boolean;
var
  GraphicClass: TGraphicClass;
  Graphic: TGraphic;
begin
  //GraphicClass := nil;
  Result := False;
  case GetImageFormat(Stream) of
    BMP: GraphicClass := Graphics.TBitmap;
    PNG: GraphicClass := TPNGImage;
    JPG: GraphicClass := TJPEGImage;
    GIF: GraphicClass := TGIFImage;
    PBMa, PGMa, PPMa, PBMb, PGMb, PPMb: GraphicClass := TNetPBM;
    XBM: GraphicClass := TXBitmap;
    XPM: GraphicClass := TXPixmap;
    SVG: GraphicClass := TSVGImage;
    else raise EInvalidGraphic.CreateFmt(SInvalidImage, []);
  end;
  if not (GraphicClass = nil) then
  begin
    Graphic := GraphicClass.Create;
    try
      Graphic.LoadFromStream(Stream);
    except
      Graphic.Free;
      raise;
    end;
    try
      Target.Assign(Graphic);
      Result := True;
    finally
      Graphic.Free;
    end;
  end;
end;

josef-b 10. Apr 2020 15:37

AW: MIME Emails Download von Bildern - unbekannter Dateityp
 
Ganz lieben Dank für Eure schnellen Antworten.

Ich versuchs mal mit

IdHTTP.Response.ContentType Property.

Bei den ersten mails, die ich eingelesen habe, klappt es schon mal
nachdem ich die Extension geändert habe


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:30 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz