Exemplo n.º 1
0
        /// <summary>
        /// Uploads a file
        /// </summary>
        /// <param name="filename">The filename we want to upload</param>
        /// <param name="MD5Checksum">The MD5 checksum of a file</param>
        /// <returns>An Image object that describes the image we uploaded</returns>
        public async Task <Image> UploadImageAsync(string filename, string MD5Checksum, CancellationToken token)
        {
            string uploadURL    = string.Format("http://upload.smugmug.com/{0}", filename);
            var    myFileInfo   = new FileInfo(filename);
            var    myWebRequest = (HttpWebRequest)WebRequest.Create(uploadURL);

            myWebRequest.UserAgent     = "YASMAPI v1.0";
            myWebRequest.ContentType   = "binary/octet-stream";
            myWebRequest.ContentLength = myFileInfo.Length;
            myWebRequest.Method        = WebRequestMethods.Http.Put;

            myWebRequest.Headers.Add("X-Smug-Version", Version);
            myWebRequest.Headers.Add("X-Smug-ResponseType", ResponseType);
            myWebRequest.Headers.Add("X-Smug-AlbumID", Album.id.ToString());
            myWebRequest.Headers.Add("Content-MD5", MD5Checksum);
            myWebRequest.Headers.Add("X-Smug-FileName", myFileInfo.Name);

            if (Altitude != null)
            {
                myWebRequest.Headers.Add("X-Smug-Altitude", Altitude.ToString());
            }
            if (Caption != null)
            {
                myWebRequest.Headers.Add("X-Smug-Caption", Caption);
            }
            if (Hidden == true)
            {
                myWebRequest.Headers.Add("X-Smug-Hidden", "true");
            }
            if (ImageID != null)
            {
                myWebRequest.Headers.Add("X-Smug-ImageID", ImageID.ToString());
            }
            if (Keywords != null)
            {
                myWebRequest.Headers.Add("X-Smug-Keywords", Keywords);
            }
            if (Latitude != null)
            {
                myWebRequest.Headers.Add("X-Smug-Latitude", Latitude.ToString());
            }
            if (Longitude != null)
            {
                myWebRequest.Headers.Add("X-Smug-Longitude", Longitude.ToString());
            }
            if (Pretty == true)
            {
                myWebRequest.Headers.Add("X-Smug-Pretty", "true");
            }


            // Add the authorization header
            myWebRequest.Headers.Add("Authorization", oAuthUtility.GetAuthorizationHeader(authentication.AccessToken, uploadURL));

            //we start reading from the file...

            //we have some elements to set
            //- request time out (compute this for 10 kb/sec speed)
            //- the chunk size to use when uploading (how much data to report after)
            if (UploadStarted != null)
            {
                UploadStarted(this, new UploadEventArgs()
                {
                    FileName = myFileInfo.FullName, PercentComplete = 0
                });
            }

            int timeOut = ((int)myFileInfo.Length / 1024) * 1000;

            myWebRequest.Timeout                   = timeOut;
            myWebRequest.ReadWriteTimeout          = timeOut;
            myWebRequest.AllowWriteStreamBuffering = AllowWriteStreamBuffering;

            long howMuchRead = 0;

            byte[] buffer    = new byte[chunkSize];
            int    readSoFar = 0;

            try
            {
                using (FileStream sr = new FileStream(myFileInfo.FullName, FileMode.Open, FileAccess.Read))
                {
                    using (var stream = await myWebRequest.GetRequestStreamAsync())
                    {
                        while (howMuchRead < myFileInfo.Length)
                        {
                            //we try to read a chunk from the file
                            readSoFar    = sr.Read(buffer, 0, chunkSize);
                            howMuchRead += readSoFar;

                            //we now write those files to the web.
                            await stream.WriteAsync(buffer, 0, readSoFar, token);

                            if (UploadProgress != null)
                            {
                                UploadProgress(this, new UploadEventArgs()
                                {
                                    FileName = myFileInfo.FullName, PercentComplete = (float)howMuchRead / (float)myFileInfo.Length
                                });
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                //an error has occured...
                throw;
            }

            var resp = await myWebRequest.GetResponseAsync();

            string rez = string.Empty;

            using (StreamReader ns = new StreamReader(resp.GetResponseStream()))
            {
                rez = await ns.ReadToEndAsync();
            }

            if (UploadCompleted != null)
            {
                UploadCompleted(this, new UploadEventArgs()
                {
                    FileName = myFileInfo.FullName, PercentComplete = (float)howMuchRead / (float)myFileInfo.Length
                });
            }

            //we deserialize the image
            var response = JsonConvert.DeserializeObject <ImageResponse>(System.Uri.UnescapeDataString(rez));

            if (response.stat == "ok")
            {
                Image img = response.Image;
                img.basic = authentication;
                img.Album = Album;

                return(img);
            }
            else
            {
                throw new SmugMugException(response.code, response.message, response.method);
            }
        }
Exemplo n.º 2
0
 public override string ToString()
 {
     return(m_id.ToString());
 }
Exemplo n.º 3
0
        protected void doStuff()
        {
            if (_Put)
            {
                Console.WriteLine("Reading: " + _FileName);

                byte[] j2cdata;

                if (_Rate != 0)
                {
                    j2cdata = KakaduWrap.ReadJ2CData(_FileName, _Rate);
                }
                else
                {
                    j2cdata = KakaduWrap.ReadJ2CData(_FileName);
                }


                _Client.Inventory.DownloadInventory();

                Console.WriteLine("Connecting to your Texture folder...");
                InventoryFolder iFolder = _Client.Inventory.getFolder("Textures");

                Console.WriteLine("Uploading Texture...");
                InventoryImage image = iFolder.NewImage(_FileName, "ImageTool Upload", j2cdata);
                Console.WriteLine("Asset id = " + image.AssetID.ToStringHyphenated());
            }
            else
            {
                foreach (LLUUID ImageID in _ImageIDs)
                {
                    string FileName;
                    if (_ImageIDs.Count > 1)
                    {
                        FileName = ImageID.ToString();
                    }
                    else
                    {
                        FileName = _FileName;
                    }

                    Console.WriteLine("Downloading: " + ImageID);

                    int    start = Environment.TickCount;
                    byte[] j2cdata;

                    try
                    {
                        j2cdata = _Client.Images.RequestImage(ImageID);

                        int end = Environment.TickCount;
                        Console.WriteLine("Elapsed download time, in TickCounts: " + (end - start));

                        Console.WriteLine("Image Data Length :" + j2cdata.Length);

                        Console.WriteLine("Writing to: " + FileName + ".tif");
                        File.WriteAllBytes(FileName + ".tif", JasperWrapper.jasper_decode_j2c_to_tiff(j2cdata));

                        Console.WriteLine("Writing to: " + FileName + ".tga");
                        File.WriteAllBytes(FileName + ".tga", JasperWrapper.jasper_decode_j2c_to_tga(j2cdata));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("ERROR: Can't download image :: " + e.Message);
                    }
                }
            }

            Console.WriteLine("Done...");
        }
Exemplo n.º 4
0
 public override string ToString() => ImageID.ToString();