mod_vcard_odbc avatar manipulation

Hey Folks:
I need to hack a solution together to resize the avatar before its stored in the DB(I know bad idea, but we need this while we work on porting some utilities to our client). I am not that familiar with erlang so I am having issue tracing this down in the code.

I believe its around the XData case in mod_vcard_odbc where it uses the jlib parser. Any tips on where to get started or what to look at would be greatly appreciated.

Thanks a bunch.

-Leigh

I've implemented the feature

I've implemented the feature in mod_vcard. I imagine you can copy the code to mod_vcard_odbc with minor changes.

This patch requires Imagemagick 'convert' program. It resizes all images to 80x80 pixels.

diff --git a/src/mod_vcard.erl b/src/mod_vcard.erl
index 77dc30b..0b8abfb 100644
--- a/src/mod_vcard.erl
+++ b/src/mod_vcard.erl
@@ -198,7 +198,39 @@ process_sm_iq(From, To, #iq{type = Type, sub_el = SubEl} = IQ) ->
 	    IQ#iq{type = result, sub_el = Els}
     end.
 
-set_vcard(User, LServer, VCARD) ->
+replace_binval(VCARD1, Photo2BinVal) ->
+    {xmlelement, Name, Attrs, Els1} = VCARD1,
+    Els = replace_binval(["PHOTO", "BINVAL"], Photo2BinVal, Els1, []),
+    {xmlelement, Name, Attrs, Els}.
+
+replace_binval(_, _, [], Res) ->
+    lists:reverse(Res);
+replace_binval([], Photo2BinVal, [{xmlcdata, _Cdata} | Els], Res) ->
+    [{xmlcdata, Photo2BinVal} | Res]++Els;
+replace_binval([], _, _, Res) ->
+    lists:reverse(Res);
+replace_binval([Name | OtherNames], Photo2BinVal, [{xmlelement, Name, Attrs, SubEls1} | Els], Res) ->
+    SubEls = replace_binval(OtherNames, Photo2BinVal, SubEls1, []),
+    El2 = {xmlelement, Name, Attrs, SubEls},
+    [El2 | Res]++Els;
+replace_binval(Names, Photo2BinVal, [El | Els], Res) ->
+    replace_binval(Names, Photo2BinVal, Els, [El | Res]).
+
+set_vcard(User, LServer, VCARD1) ->
+    Photo1BinVal = xml:get_path_s(VCARD1, [{elem, "PHOTO"}, {elem, "BINVAL"}, cdata]),
+    Photo1Img = jlib:decode_base64(Photo1BinVal),
+    F1n = "/tmp/ejabberd_vcard1_"++randoms:get_string(),
+    F2n = "/tmp/ejabberd_vcard2_"++randoms:get_string(),
+    {ok, F1d} = file:open(F1n, [write, binary]),
+    file:write(F1d, Photo1Img),
+    file:close(F1d),
+    os:cmd("convert " ++ F1n ++ " -resize 80x80 " ++ F2n),
+    {ok, Photo2Img} = file:read_file(F2n),
+    Photo2BinVal = jlib:encode_base64(binary_to_list(Photo2Img)),
+    VCARD = replace_binval(VCARD1, Photo2BinVal),
+    file:delete(F1n),
+    file:delete(F2n),
+
     FN       = xml:get_path_s(VCARD, [{elem, "FN"},                     cdata]),
     Family   = xml:get_path_s(VCARD, [{elem, "N"}, {elem, "FAMILY"},    cdata]),
     Given    = xml:get_path_s(VCARD, [{elem, "N"}, {elem, "GIVEN"},     cdata]),
Syndicate content