How to serve file referenced from HTML file using WebBroker (TIdHTTPWebBrokerBridge) and TWebModule - windows

I built tiny http server (Delphi 10.2) to return some calculations to client on-line.
I used page producer with TPageProducer.HTMLFile set to local file.
The test.html file has reference to css file on local disk like: <link rel="stylesheet" href="data/style.css"> in the head section. It works from Firefox styling my html.
To be able to serve this css file from local folder I handled WebModule.BeforeDispatch:
procedure TWebModule1.WebModuleBeforeDispatch(Sender: TObject; Request: TWebRequest;
Response: TWebResponse; var Handled: Boolean);
var
LocalPath: String;
begin
if Request.PathInfo.StartsWith('/data/') then
begin
LocalPath := DataPath + StringReplace(Request.PathInfo, '/', '\', [rfReplaceAll]);
if FileExists(LocalPath) then
begin
Response.ContentStream := TFileStream.Create(LocalPath, fmShareDenyWrite);
Assert(Response.ContentStream.Size > 0);
end;
Handled := True;
end;
end;
When I run my server and go to address: http://localhost/data/style.css
I obtain proper contents of style.css file in a browser window as a result.
Why is it not used as style for my HTML file even if it is correctly read in OnBeforeDispatch when using address like http://localhost/test.html?
It seems that one file (CSS) is referenced from other (HTML) and this makes things cluttered.

Related

How to extract EXIF metadata from BLOB column

I am trying to extract geolocation from EXIF metadata and the JPEG-image is stored in blob column in Oracle Database 12c using PLSQL.
Oracle Multimedia package is installed. The image contains data about geolocation before uploading, and when I download it again from the BLOB, latitude and longitude are there in metadata. But when I try to read it in database, I can read only basic metadata like image width, height, size, mimetype.
I am using this procedure to extract metadata from BLOB column:
PROCEDURE extractMetadata(inID IN INTEGER) IS
img ORDSYS.ORDIMAGE;
metav XMLSequenceType;
meta_root VARCHAR2(40);
xmlORD XMLType;
xmlXMP XMLType;
xmlEXIF XMLType;
xmlIPTC XMLType;
BEGIN
-- select the image
SELECT ordsys.ordimage(d.blob,1)
INTO img
FROM PHOTOS d
WHERE d.id = inID;
-- extract all the metadata
metav := img.getMetadata( 'ALL' );
-- process the result array to discover what types of metadata were returned
FOR i IN 1..metav.count() LOOP
meta_root := metav(i).getRootElement();
DBMS_OUTPUT.PUT_LINE(meta_root);
CASE meta_root
WHEN 'ordImageAttributes' THEN xmlORD := metav(i);
WHEN 'xmpMetadata' THEN xmlXMP := metav(i);
WHEN 'iptcMetadata' THEN xmlIPTC := metav(i);
WHEN 'exifMetadata' THEN xmlEXIF := metav(i);
ELSE NULL;
END CASE;
END LOOP;
-- Update metadata columns
--
update photos SET metaORDImage = xmlORD,
metaEXIF = xmlEXIF,
metaIPTC = xmlIPTC,
metaXMP = xmlXMP
WHERE id = inID;
END extractMetadata;
but only metaORDImage is extracted.
It seems like there is no EXIF metadata available, but when I download image to my PC and check details, there is location data:
What could be wrong here? Is it possible at all to extract EXIF from BLOB and should I store the image in some other format rather than BLOB?
UPDATE: I used Javascript library exif-js to read metada on page when the image is loaded into webpage, and once again geolocation is there. I can read it anywhere but not in PLSQL.
{
"ImageWidth": 720,
"ImageHeight": 1520,
"ExifIFDPointer": 74,
"Orientation": 0,
"GPSInfoIFDPointer": 92,
"LightSource": "Unknown",
"GPSLatitude": [
43,
xx,
17.861
],
"GPSLatitudeRef": "N",
"GPSLongitudeRef": "E",
"GPSLongitude": [
17,
xx,
7.29
],
"thumbnail": {}
}
My proposal would be a Java class, e.g. Apache Commons Imaging or org.w3c.tools.jpeg or metadata-extractor
First you need to load the Java classes into your database. Can be done with the loadjava tool:
loadjava -verbose -user username/password#DATABASE_NAME -r -o .\commons-imaging-1.0-alpha3.jar
Then you need to define the PL/SQL procedure/function:
CREATE OR REPLACE FUNCTION getMetadata(img IN BLOB) RETURN VARCHAR2
AS LANGUAGE JAVA NAME 'Imaging.getMetadata(byte[] bytes) return java.lang.String';
/
In the final step you can use the PL/SQL as usual. Above function does not work, because of wrong return type but I hope you get an idea how it works in general.
See Calling Java Methods in Oracle Database and https://www.tabnine.com/code/java/classes/oracle.sql.BLOB

Inno Setup to modify key values in app.config file while running setup.exe

I need to change the values of some keys in app.config file. I'm planning to use Inno Setup for this task.
I have found excellent tips of using XML parser and xpath selectors in this kind of tasks. However, I don't know what path to use to point to the key & value I want to change.
My app.config looks approximately like this:
<configuration>
<runtime>
</runtime>
<appSettings>
<add key="DefaultUrl" value="http://localhost/Some_application"/>
</appSettings>
</configuration>
So, I want to access the key "DefaultUrl" and change it's "value". How to point there?
$x("//configuration/appSettings[#key='DefaultUrl']")
doesn't seem to be correct since it returns nothing.
EDIT after Mirtheil's comment:
To change the key values I use this procedure:
procedure SaveAttributeValueToXML(const AFileName, APath, AAttribute,
AValue: string);
var
XMLNode: Variant;
XMLDocument: Variant;
begin
XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0');
try
XMLDocument.async := False;
XMLDocument.load(AFileName);
if (XMLDocument.parseError.errorCode <> 0) then
MsgBox('The XML file could not be parsed. ' +
XMLDocument.parseError.reason, mbError, MB_OK)
else
begin
XMLDocument.setProperty('SelectionLanguage', 'XPath');
XMLNode := XMLDocument.selectSingleNode(APath);
XMLNode.setAttribute(AAttribute, AValue);
XMLDocument.save(AFileName);
end;
except
MsgBox('An error occured!' + #13#10 + GetExceptionMessage,
mbError, MB_OK);
end;
end;
I call it
SaveAttributeValueToXML(Some_Application.exe.config', '//configuration/appSettings[#key=''DefaultUrl'']', 'value', 'https://Server_Name/Other_Application');
This call generates an exception on line
XMLNode.setAttribute(AAttribute, AValue);
Exception message: "Variant is null, cannot invoke"
EDIT 2:
I found the answer. Calling
SaveAttributeValueToXML(Some_Application.exe.config', '//configuration/appSettings/add[#key=''DefaultUrl'']', 'value', 'https://Server_Name/Other_Application');
changes the value of key "DefaultUrl".
I found the answer.
$x("//configuration/appSettings/add[#key='DefaultUrl']")
returns the node I want.

Lazarus - loading picture form file

I have an issue with loading a picture from z file. The picture has a .png extension.
Can anyone explain me why it doesn't work anymore ?
if (FileExists('file.png')) then
Image1.Picture.LoadFromFile('file.png');
Errors:
Project project1 raised exception class 'PNGImageException' with message: This is not PNG-data
Project project1 raised exception class 'FPImageException' with message: Wrong image format
At the first create your image component:
MyPicture := TImage.Create(FormCanvas);
MyPicture.Name := 'picture';
MyPicture.Parent := FormCanvas;
V1 - use direct path
Edit_pic_path.Text := 'C:\Images\';
Prop_Picture_Name.Text := 'image.png';
try
if (FileExists(Edit_pic_path.Text + Prop_Picture_Name.Text)) then
begin
MyPicture.Picture.LoadFromFile(Edit_pic_path.Text + Prop_Picture_Name.Text);
end;
finally
end;
V2 - image must be in project folder
...
MyPicture.Picture.LoadFromFile(Prop_Picture_Name.Text);
...

Write content of file into another file

So, i've faced this task. I wrote code, but somehow instead of putting content of file into another file2, it simply erase content of file2. What am i doing wrong?
Program Lesson9_Program2;
Var FName, Fname2, Txt, Txt2 : String;
UserFile, UserFile2 : Text;
Begin
FName := 'Textfile';
Assign(UserFile,'E:\text.txt'); {assign a text file}
Assign(UserFile2,'E:\text2.txt');
Reset(UserFile);
Reset(UserFile2);
readln(UserFile2, Txt);
readln(UserFile, Txt2);
Close(UserFile2);
Close(UserFile);
Rewrite(UserFile);
WriteLn(UserFile, Txt);
WriteLn(UserFIle, Txt2);
Close(UserFile);
Rewrite(UserFile);
End.
So the problem was in last Rewrite function. Turned out, it made to erase file content, so removing it fixed my program

How to let user to browse to a file to be copied with Inno Setup

I need user to browse to a file, select it, and then have this file copied from selected source to app folder.
Following this post
How to show/use the user selected app path {app} in InputDirPage in Inno Setup?
and Inno Setup documentation, i came to this piece of code:
[Files]
Source: {code:GetDBPath}; DestDir: "{app}"; Flags: confirmoverwrite uninsneveruninstall;
[Code]
var
SelectDBPage: TInputDirWizardPage;
DBPath: String;
procedure InitializeWizard;
begin
SelectDBPage := CreateInputDirPage(wpSelectDir, 'Select file', 'Select file', 'Select file', False, '');
SelectDBPage.Add('');
SelectDBPage.Values[0] := ExpandConstant('{src}\DB.FDB');
DBPath := SelectDBPage.Values[0];
end;
function GetDBPath():String;
begin
Result := DBPath;
end;
My problem is to retrieve file path. At instruction 'Source: {code:GetDBPath}' i get an 'Unknown filename prefix {code:' error.
How can I refer to the selected file path in [File] section?
Thank you
You need to add the external flag to the [Files] entry. This means the source will be evaluated at run time and CAN include {code:...} constants.
You're also not getting the correct value in your GetDBPath() function. You're returning the value of DBPath that isn't updated after creating the page, instead of getting the latest value form the SelectDBPage.Values[0].

Resources