Author: gonzalo
Date: 2006-08-27 17:25:23 -0400 (Sun, 27 Aug 2006)
New Revision: 64450

Added:
   trunk/google-sharp/src/Mono.Google.Picasa/XmlUtil.cs
Modified:
   trunk/google-sharp/ChangeLog
   trunk/google-sharp/src/Makefile.am
   trunk/google-sharp/src/Mono.Google.Picasa/Makefile.am
   trunk/google-sharp/src/Mono.Google.Picasa/PicasaAlbum.cs
   trunk/google-sharp/src/Mono.Google.Picasa/PicasaWeb.cs
Log:
2006-08-27 Gonzalo Paniagua Javier <[EMAIL PROTECTED]>

        * src/Mono.Google.Picasa/Makefile.am:
        * src/Makefile.am:
        * src/Mono.Google.Picasa/PicasaWeb.cs:
        * src/Mono.Google.Picasa/XmlUtil.cs:
        * src/Mono.Google.Picasa/PicasaAlbum.cs: use System.Xml to write the
        POST requests. Support the 'description' argument.



Modified: trunk/google-sharp/ChangeLog
===================================================================
--- trunk/google-sharp/ChangeLog        2006-08-27 21:22:19 UTC (rev 64449)
+++ trunk/google-sharp/ChangeLog        2006-08-27 21:25:23 UTC (rev 64450)
@@ -1,3 +1,12 @@
+2006-08-27 Gonzalo Paniagua Javier <[EMAIL PROTECTED]>
+
+       * src/Mono.Google.Picasa/Makefile.am:
+       * src/Makefile.am:
+       * src/Mono.Google.Picasa/PicasaWeb.cs:
+       * src/Mono.Google.Picasa/XmlUtil.cs:
+       * src/Mono.Google.Picasa/PicasaAlbum.cs: use System.Xml to write the
+       POST requests. Support the 'description' argument.
+
 2006-08-25 Gonzalo Paniagua Javier <[EMAIL PROTECTED]>
 
        * sample/Makefile.am:

Modified: trunk/google-sharp/src/Makefile.am
===================================================================
--- trunk/google-sharp/src/Makefile.am  2006-08-27 21:22:19 UTC (rev 64449)
+++ trunk/google-sharp/src/Makefile.am  2006-08-27 21:25:23 UTC (rev 64450)
@@ -29,7 +29,8 @@
                        Mono.Google.Picasa/PicasaWeb.cs \
                        Mono.Google.Picasa/UploadPictureException.cs \
                        Mono.Google.Picasa/UploadProgressEventArgs.cs \
-                       Mono.Google.Picasa/UploadProgressEventHandler.cs
+                       Mono.Google.Picasa/UploadProgressEventHandler.cs \
+                       Mono.Google.Picasa/XmlUtil.cs
 
 monogoogle_build_sources = $(addprefix $(srcdir)/, $(monogoogle_sources)) 
$(addprefix $(top_builddir)/src/Mono.Google/, AssemblyInfo.cs)
 

Modified: trunk/google-sharp/src/Mono.Google.Picasa/Makefile.am
===================================================================
--- trunk/google-sharp/src/Mono.Google.Picasa/Makefile.am       2006-08-27 
21:22:19 UTC (rev 64449)
+++ trunk/google-sharp/src/Mono.Google.Picasa/Makefile.am       2006-08-27 
21:25:23 UTC (rev 64450)
@@ -10,5 +10,6 @@
                PicasaWeb.cs \
                UploadPictureException.cs \
                UploadProgressEventArgs.cs \
-               UploadProgressEventHandler.cs
+               UploadProgressEventHandler.cs \
+               XmlUtil.cs
 

Modified: trunk/google-sharp/src/Mono.Google.Picasa/PicasaAlbum.cs
===================================================================
--- trunk/google-sharp/src/Mono.Google.Picasa/PicasaAlbum.cs    2006-08-27 
21:22:19 UTC (rev 64449)
+++ trunk/google-sharp/src/Mono.Google.Picasa/PicasaAlbum.cs    2006-08-27 
21:25:23 UTC (rev 64450)
@@ -143,7 +143,7 @@
                        return coll;
                }
 
-               static string op_upload = 
+               /*
                        "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
                        "<rss version=\"2.0\" 
xmlns:gphoto=\"http://www.temp.com/\";>\n" +
                        " <channel>\n" +
@@ -160,7 +160,24 @@
                        "  </item>\n" +
                        " </channel>\n" +
                        "</rss>";
+               */
 
+               static string GetXmlForUpload (string user, string aid, string 
title, string multipart_name, string description)
+               {
+                       XmlUtil xml = new XmlUtil ();
+                       xml.WriteElementString ("user", user, 
PicasaNamespaces.GPhoto);
+                       xml.WriteElementString ("id", aid, 
PicasaNamespaces.GPhoto);
+                       xml.WriteElementString ("op", 
"createAndAppendPhotoToAlbum", PicasaNamespaces.GPhoto);
+                       xml.WriteStartElement ("item");
+                       xml.WriteElementString ("title", title);
+                       xml.WriteElementString ("description", description);
+                       xml.WriteElementString ("multipart", multipart_name, 
PicasaNamespaces.GPhoto);
+                       //checksum?
+                       xml.WriteElementString ("layout", "0.000000", 
PicasaNamespaces.GPhoto);
+                       xml.WriteElementString ("client", "picasa", 
PicasaNamespaces.GPhoto); // Should not lie here.
+                       return xml.GetDocumentString ();
+               }
+
                static string disp_pic = "Content-Disposition: form-data; 
name=\"{0}\"; filename=\"{1}\"\r\n";
 
                public string UploadPicture (string title, Stream input)
@@ -180,6 +197,9 @@
                                throw new ArgumentException ("Cannot read from 
stream", "input");
 
                        string url = API.GetPostURL ();
+                       if (url == null)
+                               throw new UnauthorizedAccessException ("You are 
not authorized to upload to this album.");
+
                        MultipartRequest request = new MultipartRequest (url);
                        request.Request.CookieContainer = conn.Cookies;
                        MemoryStream ms = null;
@@ -193,13 +213,12 @@
                        request.BeginPart ();
                        request.AddHeader ("Content-Disposition: form-data; 
name=\"xml\"\r\n");
                        request.AddHeader ("Content-Type: text/plain; 
charset=utf8\r\n", true);
-                       string upload = String.Format (op_upload, 
Connection.User, UniqueID, title, title.GetHashCode ().ToString ());
-                       if (description != null && description != String.Empty)
-                               upload = upload.Replace ("   <description/>", 
String.Format ("   <description>{0}</description>", description));
+                       string multipart_name = title.GetHashCode ().ToString 
();
+                       string upload = GetXmlForUpload (Connection.User, 
UniqueID, title, multipart_name, description);
                        request.WriteContent (upload);
                        request.EndPart (false);
                        request.BeginPart ();
-                       request.AddHeader (String.Format (disp_pic, 
title.GetHashCode ().ToString (), title));
+                       request.AddHeader (String.Format (disp_pic, 
multipart_name, title));
                        request.AddHeader ("Content-Type: 
application/octet-stream\r\n", true);
 
                        byte [] data = new byte [8192];

Modified: trunk/google-sharp/src/Mono.Google.Picasa/PicasaWeb.cs
===================================================================
--- trunk/google-sharp/src/Mono.Google.Picasa/PicasaWeb.cs      2006-08-27 
21:22:19 UTC (rev 64449)
+++ trunk/google-sharp/src/Mono.Google.Picasa/PicasaWeb.cs      2006-08-27 
21:25:23 UTC (rev 64450)
@@ -150,39 +150,56 @@
                        return doc.SelectSingleNode ("/response/id").InnerText;
                }
 
-               static string create_album_op =
-                               "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" 
+
-                               "<rss version=\"2.0\" 
xmlns:gphoto=\"http://www.temp.com/\";>\n" +
-                               " <channel>\n" +
-                               "  <title>{0}</title>\n" +
-                               "  <description/>\n" +
-                               "  <pubDate>{1:d' 'MMM' 'yyyy' 'HH':'mm':'ss' 
'zzz}</pubDate>\n" +
-                               "  <gphoto:access>{2}</gphoto:access>\n" +
-                               "  <gphoto:user>{3}</gphoto:user>\n" +
-                               "  <gphoto:location/>\n" +
-                               "  <gphoto:op>createAlbum</gphoto:op>\n" +
-                               " </channel>\n" +
-                               "</rss>";
+               /*
+                       "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
+                       "<rss version=\"2.0\" 
xmlns:gphoto=\"http://www.temp.com/\";>\n" +
+                       " <channel>\n" +
+                       "  <title>{0}</title>\n" +
+                       "  <description/>\n" +
+                       "  <pubDate>{1:d' 'MMM' 'yyyy' 'H':'mm':'ss' 
'zzz}</pubDate>\n" +
+                       "  <gphoto:access>{2}</gphoto:access>\n" +
+                       "  <gphoto:user>{3}</gphoto:user>\n" +
+                       "  <gphoto:location/>\n" +
+                       "  <gphoto:op>createAlbum</gphoto:op>\n" +
+                       " </channel>\n" +
+                       "</rss>";
+               */
 
                static string GetXmlForCreate (string title, string desc, 
DateTime date, AlbumAccess access, string username)
                {
-                       string acc = access.ToString ().ToLower 
(CultureInfo.InvariantCulture);
-                       string result = String.Format (create_album_op, title, 
date, acc, username).Replace (":00", "00");
-                       if (desc != null && (desc = desc.Trim ()) != "")
-                               result = result.Replace ("  <description/>", 
String.Format ("  <description>{0}</description>", desc));
-                       return result;
+                       XmlUtil xml = new XmlUtil ();
+                       xml.WriteElementString ("title", title);
+                       xml.WriteElementString ("description", desc);
+                       string pubdate = date.ToString ("d' 'MMM' 'yyyy' 
'H':'mm':'ss' 'zzz");
+                       pubdate = pubdate.Substring (0, pubdate.Length - 3) + 
"00"; // Replaces ':00' with '00'
+                       xml.WriteElementString ("pubDate", pubdate);
+                       xml.WriteElementString ("access", access.ToString 
().ToLower (CultureInfo.InvariantCulture), PicasaNamespaces.GPhoto);
+                       xml.WriteElementString ("user", username, 
PicasaNamespaces.GPhoto);
+                       xml.WriteElementString ("op", "createAlbum", 
PicasaNamespaces.GPhoto);
+                       // location?
+                       return xml.GetDocumentString ();
                }
 
-               static string delete_album_op =
-                               "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" 
+
-                               "<rss version=\"2.0\" 
xmlns:gphoto=\"http://www.temp.com/\";>\n" +
-                               "  <channel>\n" +
-                               "    <gphoto:user>{0}</gphoto:user>\n" +
-                               "    <gphoto:id>{1}</gphoto:id>\n" +
-                               "    <gphoto:op>deleteAlbum</gphoto:op>\n" +
-                               "  </channel>\n" +
-                               "</rss>";
+               /*
+                       "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
+                       "<rss version=\"2.0\" 
xmlns:gphoto=\"http://www.temp.com/\";>\n" +
+                       "  <channel>\n" +
+                       "    <gphoto:user>{0}</gphoto:user>\n" +
+                       "    <gphoto:id>{1}</gphoto:id>\n" +
+                       "    <gphoto:op>deleteAlbum</gphoto:op>\n" +
+                       "  </channel>\n" +
+                       "</rss>";
+               */
 
+               static string GetXmlForDelete (string user, string aid)
+               {
+                       XmlUtil xml = new XmlUtil ();
+                       xml.WriteElementString ("user", user, 
PicasaNamespaces.GPhoto);
+                       xml.WriteElementString ("id", aid, 
PicasaNamespaces.GPhoto);
+                       xml.WriteElementString ("op", "deleteAlbum", 
PicasaNamespaces.GPhoto);
+                       return xml.GetDocumentString ();
+               }
+
                public void DeleteAlbum (PicasaAlbum album)
                {
                        if (album == null)
@@ -197,7 +214,7 @@
                                throw new ArgumentNullException ("unique_id");
 
                        string url = api.GetPostURL ();
-                       string op_string = String.Format (delete_album_op, 
conn.User, unique_id);
+                       string op_string = GetXmlForDelete (conn.User, 
unique_id);
                        byte [] op_bytes = Encoding.UTF8.GetBytes (op_string);
                        MultipartRequest request = new MultipartRequest (url);
                        request.Request.CookieContainer = conn.Cookies;

Added: trunk/google-sharp/src/Mono.Google.Picasa/XmlUtil.cs
===================================================================
--- trunk/google-sharp/src/Mono.Google.Picasa/XmlUtil.cs        2006-08-27 
21:22:19 UTC (rev 64449)
+++ trunk/google-sharp/src/Mono.Google.Picasa/XmlUtil.cs        2006-08-27 
21:25:23 UTC (rev 64450)
@@ -0,0 +1,99 @@
+//
+// Mono.Google.Picasa.XmlUtil.cs:
+//
+// Authors:
+//     Gonzalo Paniagua Javier ([EMAIL PROTECTED])
+//
+// (C) Copyright 2006 Novell, Inc. (http://www.novell.com)
+//
+
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.IO;
+using System.Text;
+using System.Xml;
+namespace Mono.Google.Picasa {
+       enum PicasaNamespaces {
+               None,
+               GPhoto,
+               Photo,
+               Media
+       }
+
+       class XmlUtil {
+               static string [] URIs = new string [] {
+                       null,
+                       "http://picasaweb.google.com/lh/picasaweb/";,
+                       "http://www.pheed.com/pheed/";,
+                       "http://search.yahoo.com/mrss/";
+               };
+
+               StringWriter sr;
+               XmlTextWriter writer;
+
+               public XmlUtil ()
+               {
+                       StartDocument ();
+               }
+
+               public void StartDocument ()
+               {
+                       sr = new StringWriter ();
+                       writer = new XmlTextWriter (sr);
+                       writer.Formatting = Formatting.Indented;
+                       writer.Indentation = 2;
+                       writer.Namespaces = true;
+                       writer.WriteProcessingInstruction ("xml", 
"version='1.0' encoding='utf-8'");
+                       writer.WriteStartElement (null, "rss", null);
+                       writer.WriteAttributeString ("version", "2.0");
+                       writer.WriteAttributeString ("xmlns", "gphoto", null, 
URIs [(int) PicasaNamespaces.GPhoto]);
+                       writer.WriteStartElement ("channel", null);
+               }
+
+               public void WriteElementString (string name, string val)
+               {
+                       writer.WriteElementString (name, val);
+               }
+
+               public void WriteElementString (string name, string val, string 
uri)
+               {
+                       writer.WriteElementString (name, uri, val);
+               }
+
+               public void WriteElementString (string name, string val, 
PicasaNamespaces ns)
+               {
+                       writer.WriteElementString (name, URIs [(int) ns], val);
+               }
+
+               public void WriteStartElement (string elem)
+               {
+                       writer.WriteStartElement (elem, null);
+               }
+
+               public string GetDocumentString ()
+               {
+                       writer.Close ();
+                       return sr.ToString ();
+               }
+       }
+}
+


Property changes on: trunk/google-sharp/src/Mono.Google.Picasa/XmlUtil.cs
___________________________________________________________________
Name: svn:eol-style
   + native

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to