People have created various versions where you can import image resources in a NSF database using LotusScript. Basically you mimic the functionality of "Import Image Resource" button present in Domino Designer via code.
Some of the examples can be found
here and
here. Here is my version of doing this same thing using combination of LotusSciript & Java with LS2J.
First, download the ZIP file for
Apache Commons Codec. For this example I am using Apache Commons Codec 1.4. Extract the downloaded zip file on to local file system. Then include the jar file "commons-codec-1.4.jar" in the database in a Java script library.
Create a class inside the script library, say
ApacheCommons. Put the following code in it:
import java.io.RandomAccessFile;
import org.apache.commons.codec.binary.*; //This package contains class to convert bytes to Base64
public class ApacheCommons {
public static String getBase64(String imageFileName) {
String returnValue = "";
try {
// Read the bytes from image file
RandomAccessFile raf = new RandomAccessFile(imageFileName, "r");
byte bytImageFile[] = new byte[(int) (raf.length())];
raf.readFully(bytImageFile, 0, bytImageFile.length);
raf.close();
// Convert the bytes read from image file into Base64
byte byteBase64[] = Base64.encodeBase64(bytImageFile);
// Return the Base64 encoded image data
returnValue = new String(byteBase64);
} catch (Exception e) {
returnValue = "";
}
return returnValue;
}
}
Now create an agent in LotusScript and put the following code in it:
Option Public
Option Declare
UseLSX "*javacon" 'Use LS2J to call the Java class ApacheCommons
Use "ApacheCommons" 'Use the ApacheCommons Java script library
Sub Initialize()
On Error GoTo ErrorHandler
Dim session As New NotesSession
Dim workspace As New NotesUIWorkspace
Dim currentDatabase As NotesDatabase
Dim path As String, imageFile As Variant, imageFileName As String, imageBase64 As String
Dim imageExtension As String, imageStartTag As String, imageEndTag As String
Dim imageResourceDXL As String
Dim inStream As NotesStream, outStream As NotesStream
Dim DXLImporter As NotesDXLImporter
Dim jSession As New JavaSession
Dim apacheCommons As JavaClass
Set currentDatabase = session.Currentdatabase
'Get the path where the image's DXL file will be created
path = session.Getenvironmentstring("Directory", True)
If Right(path, 1) <> "\" Then path = path + "\"
path = path + "imageToBeImported.xml"
'Show file dialog so that user can select JPEG/JPG/GIF file
imageFile = workspace.Openfiledialog(false, "Import Image Resource", "JPEG|*.jpeg|JPG|*.jpg|GIF|*.gif")
If IsEmpty(imageFile) Then Exit Sub
'Depending on files extension create tags for JPG/GIF
imageFileName = Trim(StrRightBack(imageFile(0), "\"))
imageExtension = UCase(Trim(StrRightBack(imageFile(0), ".")))
If imageExtension = "JPEG" Or imageExtension = "JPG" Then
imageStartTag = "<jpeg>"
imageEndTag = "</jpeg>"
ElseIf imageExtension = "GIF" Then
imageStartTag = "<gif>"
imageEndTag = "</gif>"
Else
MsgBox "Only JPEG/JPG/GIF image file can be imported.", 0+16, "ERROR"
Exit Sub
End If
'Get the Base64 encoding for the image file using ApacheCommons (Java) script library
Set apacheCommons = jSession.GetClass("ApacheCommons")
imageBase64 = apacheCommons.getBase64(imageFile(0))
'Create XML file of image to be uploaded
imageResourceDXL = |<?xml version="1.0" encoding="utf-8" ?>|
imageResourceDXL = imageResourceDXL + |<database xmlns="http://www.lotus.com/dxl">|
imageResourceDXL = imageResourceDXL + |<imageresource name="| + imageFileName + |" noreplace="false">|
imageResourceDXL = imageResourceDXL + imageStartTag
imageResourceDXL = imageResourceDXL + imageBase64
imageResourceDXL = imageResourceDXL + imageEndTag
imageResourceDXL = imageResourceDXL + "</imageresource></database>"
'Write the XML file on to local file system
Set outStream = session.Createstream()
If Not outStream.Open(path) Then
MsgBox "Unable to open file to write DXL.", 0+16, "ERROR"
Exit Sub
End If
Call outStream.Truncate()
outStream.Writetext(imageResourceDXL)
Call outStream.Close()
'Import the XML file in current database
Set inStream = session.Createstream()
If Not inStream.Open(path) Then
MsgBox "Unable to open file to read DXL.", 0+16, "ERROR"
Exit Sub
End If
Set DXLImporter = session.Createdxlimporter(inStream, currentDatabase)
DXLImporter.Designimportoption = DXLIMPORTOPTION_REPLACE_ELSE_CREATE
DXLImporter.ReplicaRequiredForReplaceOrUpdate = false
Call DXLImporter.Process()
Call inStream.Close()
'Delete the XML file from local file system
Kill path
MsgBox imageFileName + " successfully imported.", 0+64, "Import Image Resource"
Exit Sub
ErrorHandler:
MsgBox "Error: " + Error$ + " Line number: " + CStr(Erl), 0+16, "ERROR"
If Not (DXLImporter Is Nothing) Then
MsgBox "Importer log: " + Chr(13) + chr(13) + DXLImporter.log, 0+64, "Importer Log"
End If
Exit Sub
End Sub
Run this agent from Actions menu in Lotus Notes client to import image resource.
Hi, does this work in XPages?
ReplyDeleteYou would have to convert the LotusScript code to Java and then call it from XPages, probably SSJS code. It would be a bit tricky as image would have to be uploaded via browser and processed, but its certainly doable.
Delete