Logo Search packages:      
Sourcecode: f-spot version File versions  Download package

string FlickrNet::Flickr::UploadPicture ( string  filename,
Stream  stream,
string  title,
string  description,
string  tags,
int  isPublic,
int  isFamily,
int  isFriend 
) [inline, private]

Private method that does all the uploading work.

Parameters:
filename The filename of the file to be uploaded.
stream The Stream object containing the pphoto to be uploaded.
title The title of the photo (optional).
description The description of the photograph (optional).
tags The tags for the photograph (optional).
isPublic 0 for private, 1 for public.
isFamily 1 if family, 0 is not.
isFriend 1 if friend, 0 if not.
Returns:
The id of the photograph after successful uploading.

Definition at line 765 of file Flickr.cs.

References OnUploadProgress, and Proxy.

            {
                  /*
                   * 
                   * Modified UploadPicture code taken from the Flickr.Net library
                   * URL: http://workspaces.gotdotnet.com/flickrdotnet
                   * It is used under the terms of the Common Public License 1.0
                   * URL: http://www.opensource.org/licenses/cpl.php
                   * 
                   * */

                  string boundary = "FLICKR_MIME_" + DateTime.Now.ToString("yyyyMMddhhmmss");

                  HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(_uploadUrl);
                  req.UserAgent = "Mozilla/4.0 FlickrNet API (compatible; MSIE 6.0; Windows NT 5.1)";
                  req.Method = "POST";
                  req.Proxy = Proxy;
                  req.Referer = "http://www.flickr.com";
                  req.ContentType = "multipart/form-data; boundary=\"" + boundary + "\"";

                  StringBuilder sb = new StringBuilder();

                  NameValueCollection parameters = new NameValueCollection();
            
                  if( title != null && title.Length > 0 )
                  {
                        parameters.Add("title", title);
                  }
                  if( description != null && description.Length > 0 )
                  {
                        parameters.Add("description", description);
                  }
                  if( tags != null && tags.Length > 0 )
                  {
                        parameters.Add("tags", tags);
                  }
                  if( isPublic >= 0 )
                  {
                        parameters.Add("is_public", isPublic.ToString());
                  }
                  if( isFriend >= 0 )
                  {
                        parameters.Add("is_friend", isFriend.ToString());
                  }
                  if( isFamily >= 0 )
                  {
                        parameters.Add("is_family", isFamily.ToString());
                  }

                  parameters.Add("api_key", _apiKey);
                  parameters.Add("auth_token", _apiToken);

                  string[] keys = parameters.AllKeys;
                  string hash = _sharedSecret;
                  Array.Sort(keys);

                  foreach(string key in keys)
                  {
                        hash += key + parameters[key];

                        sb.Append("--" + boundary + "\r\n");
                        sb.Append("Content-Disposition: form-data; name=\"" + key + "\"\r\n");
                        sb.Append("\r\n");
                        sb.Append(parameters[key] + "\r\n");
                  }

                  sb.Append("--" + boundary + "\r\n");
                  sb.Append("Content-Disposition: form-data; name=\"api_sig\"\r\n");
                  sb.Append("\r\n");
                  sb.Append(Md5Hash(hash) + "\r\n");

                  // Photo
                  sb.Append("--" + boundary + "\r\n");
                  sb.Append("Content-Disposition: form-data; name=\"photo\"; filename=\"" + filename + "\"\r\n");
                  sb.Append("Content-Type: image/jpeg\r\n");
                  sb.Append("\r\n");

                  UTF8Encoding encoding = new UTF8Encoding();

                  byte[] postContents = encoding.GetBytes(sb.ToString());
                  
                  byte[] photoContents = new byte[stream.Length];
                  stream.Read(photoContents, 0, photoContents.Length);
                  stream.Close();

                  byte[] postFooter = encoding.GetBytes("\r\n--" + boundary + "--\r\n");

                  byte[] dataBuffer = new byte[postContents.Length + photoContents.Length + postFooter.Length];
                  Buffer.BlockCopy(postContents, 0, dataBuffer, 0, postContents.Length);
                  Buffer.BlockCopy(photoContents, 0, dataBuffer, postContents.Length, photoContents.Length);
                  Buffer.BlockCopy(postFooter, 0, dataBuffer, postContents.Length + photoContents.Length, postFooter.Length);

                  req.ContentLength = dataBuffer.Length;

                  Stream resStream = req.GetRequestStream();

                  int j = 1;
                  int uploadBit = Math.Min(dataBuffer.Length / 1024, 50*1024);
                  int uploadSoFar = 0;

                  for(int i = 0; i < dataBuffer.Length; i=i+uploadBit)
                  {
                        int toUpload = Math.Min(uploadBit, dataBuffer.Length - i);
                        uploadSoFar += toUpload;

                        resStream.Write(dataBuffer, i, toUpload);

                        if( (OnUploadProgress != null) && ((j++) % 50 == 0 || uploadSoFar == dataBuffer.Length) )
                        {
                              OnUploadProgress(this, new UploadProgressEventArgs(i+toUpload, uploadSoFar == dataBuffer.Length));
                        }
                  }
                  resStream.Close();

                  HttpWebResponse res = (HttpWebResponse)req.GetResponse();

                  XmlSerializer serializer = _uploaderSerializer;

                  StreamReader sr = new StreamReader(res.GetResponseStream());
                  string s= sr.ReadToEnd();
                  sr.Close();

                  StringReader str = new StringReader(s);

                  FlickrNet.Uploader uploader = (FlickrNet.Uploader)serializer.Deserialize(str);
                  
                  if( uploader.Status == ResponseStatus.OK )
                  {
                        return uploader.PhotoId;
                  }
                  else
                  {
                        throw new FlickrException(uploader.Code, uploader.Message);
                  }
            }


Generated by  Doxygen 1.6.0   Back to index