suityou01
Veteran
Registered:1161108062 Posts: 213
Posted 1162814339
Reply with quote
#1
Hello,
I need to scoop the contents of the eAttachment table to the HDD. I have written some code to create one file per blob etc but the blobs (in this case PDF files) do not open. I then learnt that the attachments are stored in UUENCODED format in the database (at least this is the registry setting). I have found a piece of code to UUDECODE and yet the file will still not open. Does anyone have any ideas?
Doogal
Guru
Registered:1158219110 Posts: 1,564
Posted 1162815730
Reply with quote
#2
If you're talking about folder attachments, these are base-64 encoded (not sure if this is the same as UUEncosing or not). But to further complicate matters they are prefixed with a GUID (no idea why). The FreeFlow library has support for saving attachments to file, if you're using .NET.
suityou01
Veteran
Registered:1161108062 Posts: 213
Posted 1162824990
Reply with quote
#3
According to the Metastorm docs they are either UUencoded, or MIME encoded depending what you select when you install. The setting is kept in the registry, and ours is set to UUEncode. I shall investigate what you suggest.
suityou01
Veteran
Registered:1161108062 Posts: 213
Posted 1162825858
Reply with quote
#4
I have tried decoding to base 64, and it still doesn't work. What is this GUID? How many characters at the start of the file should I be counting for?
Doogal
Guru
Registered:1158219110 Posts: 1,564
Posted 1162826302
Reply with quote
#5
When the docs talk about UUEncoding and MIME encoding they are referring to email attachments.
As to the length of the GUID, it is 38 characters long. If you're dealing with a Unicode string it will be 76 bytes in length.
Hope this helps.
suityou01
Veteran
Registered:1161108062 Posts: 213
Posted 1162826990
Reply with quote
#6
So I have this blob, and chuck it out to the hard disk, myfile.pdf say. I go to open it and adobe acrobat tells me it is not a valid file. I have tried decoding from UU and base 64 and still get the same message.
Can you talk me through what I should do?
Doogal
Guru
Registered:1158219110 Posts: 1,564
Posted 1162829072
Reply with quote
#7
You need to pull the data out of the eAttachment table, remove the first 76 bytes, then base64 decode it, then save to disk. My C# code looks like this (though it probably needs to be optimised for large attachments)
Quote:
public
byte [] GetAttachment(AttachmentType type, string file, string owner) {
using (IDataReader reader = ExecuteReader(
string .Format( "SELECT eContents FROM eAttachment WHERE eKey = '{0}\t{1}\t{2}'" , ( int )type, owner, file))) {
if (reader.Read()) {
if (type == AttachmentType.Folder) {
// remove GUID
const int guidLength = 38*2;
byte [] data = ( byte [])reader[0];
byte [] truncatedData = new byte [data.Length-guidLength]; Array.Copy(data, guidLength, truncatedData, 0, truncatedData.Length);
// base64 unencode
string truncatedString = Encoding.Unicode.GetString(truncatedData);
return Convert.FromBase64String(truncatedString); }
else
return ( byte [])reader[0]; }
}
return null ; }
public
void SaveToFile( string fileName) {
byte [] data = GetAttachment(type, fileName, owner);
using (FileStream writer = new FileStream(fileName, FileMode.Create)) {
writer.Write(data, 0, data.Length);
}
}
suityou01
Veteran
Registered:1161108062 Posts: 213
Posted 1162893116
Reply with quote
#8
A wonderful answer. Muchly appreciated.
jfile
Member
Registered:1158772729 Posts: 39
Posted 1215707988
Reply with quote
#9
You are a life saver Doogal! Thanks so much for the response!
Doogal
Guru
Registered:1158219110 Posts: 1,564
Posted 1215721463
Reply with quote
#10
Cheers. There's actually a slight problem with that code. Some recent version of e-Work altered the way attachments were stored. You'll need to change it to
Quote:
byte [] truncatedData;
if (data[data.Length - 2] == 0)truncatedData =
new byte [data.Length - guidLength - 2];
else
truncatedData =
new byte [data.Length - guidLength];
Array .Copy(data, guidLength, truncatedData, 0, truncatedData.Length);
njunsved
Member
Registered:1207751993 Posts: 10
Posted 1222076358
Reply with quote
#11
Edit: Problem solved. I just looked at the string and all "truncatedString" values that contained non base64 characters I saved to file directly.
This works like a charm! A problem I'm having though is that I'm interacting with an ework database that has eContent which are not all unicode strings. Anyone that has any suggestion about how to runtime differentiate between which are unicode or not?
jfrancis
New Member
Registered:1454596522 Posts: 6
Posted 1520238975
Reply with quote
#12
Hi I realise this is a really old topic but I was wondering if anyone can help me getting the code to work? Am I meant to be using some sort of metastorm library to use this code? There's quite a few missing references, most of which are System.IO or similar, but a few others that I can't locate. Any help appreciated Thanks
Doogal
Guru
Registered:1158219110 Posts: 1,564
Posted 1520281331
Reply with quote
#13
This takes me back... I think it's all standard .NET stuff (probably need references to System.Data and System.Text) except for AttachmentType which is defined as/// <summary>Type of an attachment.</summary> public enum AttachmentType { /// <summary>Folder attachment.</summary> Folder, /// <summary>Service attachment.</summary> Service, /// <summary>Map attachment.</summary> Map, /// <summary>Procedure attachment.</summary> Procedure }
ExecuteReader is a wrapper around SqlCommand.ExecuteReader The code originally came from the long abandoned FreeFlow project which you can still download from herehttps://archive.codeplex.com/?p=freeflow
jfrancis
New Member
Registered:1454596522 Posts: 6
Posted 1520288869
Reply with quote
#14
Thanks Doogal! I appreciate the reply to such an old topic. it was the attachment type it was complaining about. I'll see if I can get it working tomorrow. Handy to know that I can just use sqlcommand as well. I was trying to do it using SQL but not had much luck other than pulling out the eAttachment table details. My query is hanging on extracting the file content from eContent. Ideally I'd like to get ALL the attachments from eattachment and dump them into a folder in readiness for migrating away from Metastorm. Also, I did download the freeflow project but I got stuck trying to build it. It was complaining about missing freeflow DLLs and 'EEVENTMANAGERMGRLib'. I might be doing it completely wrong - had to guess!
BMellert
Guru
Registered:1225130168 Posts: 688
Posted 1520345053
Reply with quote
#15
If you are looking to dump attachments, you could create a process to loop through them, use Mstm.GetAttachment(args ) which includes the patch/name to write them to. We do this for daily notices for multiple folders, when conditions warrant for that particular email. We also use a byte array to process files we get from a web service, then use System.IO.File.WriteAllBytes(args ) to write it to disk so we can display it. I've never tried the SQL Execute reader, but it's something I may keep in the mind for the future.