shaunthasheep
2007-10-14 12:38:19
program New;
function UploadImageShack(FilePath, FileName : string) : string;
var
FileContents, UploadRequest : string;
RandomString32, strBody, Data, s : string;
InFile, i, Connection : integer;
TimeOut : integer;
begin
//Thanx Cruel!
InFile:= OpenFile(FilePath + '\' + FileName, False);
if(InFile > -1)then
begin
ReadFileString(InFile, FileContents, FileSize(InFile));
CloseFile(InFile);
for i := 1 to 32 do
begin
case Random(3) + 1 of
1 : RandomString32 := RandomString32 + Chr(RandomRange(97, 122));
2 : RandomString32 := RandomString32 + Chr(RandomRange(65, 90));
3 : RandomString32 := RandomString32 + Chr(RandomRange(48, 57));
end;
end;
strBody := '--' + RandomString32 + chr(13) + chr(10);
strBody := strBody + 'Content-Disposition: form-data; name="fileupload"; filename="' + FileName + '"' + chr(13) + chr(10);
strBody := strBody + 'Content-Type: multipart/form-data' + chr(13) + chr(10);
strBody := strBody + chr(13) + chr(10) + FileContents;
strBody := strBody + chr(13) + chr(10) + '--' + RandomString32 + '--';
UploadRequest := 'POST /? HTTP/1.0' + chr(13) + chr(10);
UploadRequest := UploadRequest + 'Host: imageshack.us' + chr(13) + chr(10);
UploadRequest := UploadRequest + 'Content-Type: multipart/form-data, boundary=' + RandomString32 + chr(13) + chr(10);
UploadRequest := UploadRequest + 'Content-Length: ' + inttostr(Length(strBody)) + chr(13) + chr(10) + chr(13) + chr(10);
UploadRequest := UploadRequest + StrBody;
Connection := OpenConnection('imageshack.us', 80, 10000);
if Connection >= 0 then
begin
SendConnectionData(Connection, UploadRequest);
TimeOut := GetSystemTime;
repeat
if IsConnectionOpen(Connection) then
begin
ReadConnectionData(Connection, s);
Data := Data + s;
end else
begin
if Data <> nil then
begin
if Pos('<b>Show</b>', Data) > 0 then
begin
Delete(Data, 1, Pos('<b>Show</b>', Data));
if Pos('http://', Data) > 0 then
begin
Delete(Data, 1, Pos('http://', Data) - 1);
Result := Copy(Data, 1, Pos('"', Data) - 1);
end;
end;
end;
Exit;
end;
until GetSystemTime - TimeOut > 60000
Writeln('Timeout error');
end;
end;
end;
var
a : integer;
begin
a := GetSystemTime;
//if UploadImageShack(AppPath, 'scr.bmp') = nil then writeln('fail!');
Writeln(UploadImageShack(AppPath, 'scr.bmp'));
writeln(inttostr(getsystemtime-a));
end.Works, need to change AppPath an scr.bmp to a real image file.
Also if you want you can add a image file size check, not hard :p.