예제 #1
0
        private void SetAssets()
        {
            List <object> ex = null;

            Assets.Clear();
            if (TryGetMetadataValue("assets", out ex))
            {
                foreach (ExpandoObject ob in ex)
                {
                    AmazonFile f = new AmazonFile(string.Empty, FS)
                    {
                        Parent = null
                    };
                    f.SetData(JsonConvert.SerializeObject(ob));
                    Assets.Add(f);
                }
            }
        }
예제 #2
0
        public async Task <FileSystemResult> CopyAsync(IDirectory destination)
        {
            if (Parent == null)
            {
                return(new FileSystemResult("Unable to Copy root directory"));
            }
            if (!(destination is AmazonDirectory))
            {
                return(new FileSystemResult("Destination should be an Amazon Cloud Drive Directory"));
            }
            AmazonDirectory dest = (AmazonDirectory)destination;

            if (dest.Id == ((AmazonDirectory)Parent).Id)
            {
                return(new FileSystemResult("Source Directory and Destination Directory should be different"));
            }
            string url = AmazonCopy.FormatRest(FS.OAuth.EndPoint.MetadataUrl, dest.Id, Id);

            FileSystemResult <string> ex = await FS.OAuth.CreateMetadataStream <string>(url, null, null, HttpMethod.Put);

            if (ex.IsOk)
            {
                if (this is AmazonFile)
                {
                    AmazonFile f = new AmazonFile(destination.FullName, this.FS);
                    f.SetData(this.Metadata, this.MetadataExpanded, this.MetadataMime);
                    f.Parent = destination;
                    dest.IntFiles.Add(f);
                }
                else if (this is AmazonDirectory)
                {
                    AmazonDirectory d = new AmazonDirectory(destination.FullName, this.FS);
                    d.SetData(this.Metadata, this.MetadataExpanded, this.MetadataMime);
                    d.Parent = destination;
                    dest.IntDirectories.Add(d);
                    FS.Refs[d.FullName] = d;
                }
                return(new FileSystemResult());
            }
            return(new FileSystemResult <IDirectory>(ex.Error));
        }
예제 #3
0
        // ReSharper disable once CyclomaticComplexity
        internal async Task <FileSystemResult <IFile> > InternalCreateFile(string name, string type, bool overwrite, AmazonObject parent, Stream readstream, CancellationToken token, IProgress <FileProgress> progress, Dictionary <string, object> properties)
        {
            if (readstream.Length == 0)
            {
                throw new ArgumentException("input stream must have length");
            }
            string url = AmazonUpload.FormatRest(FS.OAuth.EndPoint.ContentUrl, "?suppress=deduplication");

            Json.Metadata j = null;
            if (!overwrite)
            {
                if (properties == null)
                {
                    properties = new Dictionary <string, object>();
                }
                j                             = new Json.Metadata();
                j.version                     = 0;
                j.contentProperties           = new Json.ContentProperties();
                j.contentProperties.size      = readstream.Length;
                j.contentProperties.version   = 0;
                j.contentProperties.extension = Path.GetExtension(name);
                string n = Extensions.ContentFromExtension(j.contentProperties.extension);
                j.contentProperties.contentType = !string.IsNullOrEmpty(n) ? n : "application/octet-stream";
                if (properties.Any(a => a.Key.Equals("ModifiedDate", StringComparison.InvariantCultureIgnoreCase)))
                {
                    j.modifiedDate =
                        (DateTime)
                        properties.First(
                            a => a.Key.Equals("ModifiedDate", StringComparison.InvariantCultureIgnoreCase)).Value;
                }
                if (properties.Any(a => a.Key.Equals("CreatedDate", StringComparison.InvariantCultureIgnoreCase)))
                {
                    j.createdDate =
                        (DateTime)
                        properties.First(
                            a => a.Key.Equals("CreatedDate", StringComparison.InvariantCultureIgnoreCase)).Value;
                }
                if (properties.Any(a => a.Key.Equals("Application", StringComparison.InvariantCultureIgnoreCase)))
                {
                    j.createdBy =
                        (string)
                        properties.First(
                            a => a.Key.Equals("Application", StringComparison.InvariantCultureIgnoreCase)).Value;
                }
                else
                {
                    j.createdBy = "CloudFileSystem";
                }
                if (properties.Any(a => a.Key.Equals("MD5", StringComparison.InvariantCultureIgnoreCase)))
                {
                    j.contentProperties.md5 =
                        (string)
                        properties.First(a => a.Key.Equals("MD5", StringComparison.InvariantCultureIgnoreCase))
                        .Value;
                }
                j.description = j.name = name;
                j.isShared    = false;
                j.kind        = type;
                j.parents     = new List <string>();
                j.parents.Add(parent.Id);
            }
            HttpRequestMessage msg      = new HttpRequestMessage(overwrite ? HttpMethod.Put : HttpMethod.Post, url);
            string             boundary = "--" + Guid.NewGuid().ToString();

            msg.Headers.UserAgent.ParseAdd(FS.OAuth.UserAgent);
            MultipartFormDataContent ct = new MultipartFormDataContent(boundary);

            if (!overwrite)
            {
                string        meta = JsonConvert.SerializeObject(j);
                StringContent sc   = new StringContent(meta, Encoding.UTF8);
                ct.Add(sc, "metadata");
            }
            StreamContent ssc = new StreamContent(readstream);

            ct.Add(ssc, "file", name);
            msg.Content = ct;
            HttpClientHandler      handler         = new HttpClientHandler();
            ProgressMessageHandler progresshandler = new ProgressMessageHandler(handler);

            progresshandler.HttpSendProgress += (a, b) =>
            {
                FileProgress u = new FileProgress();
                u.Percentage   = b.ProgressPercentage;
                u.TotalSize    = b.TotalBytes ?? 0;
                u.TransferSize = b.BytesTransferred;
                progress.Report(u);
            };
            handler.AllowAutoRedirect      = true;
            handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            await FS.CheckExpirations();

            HttpClient          cl       = new HttpClient(progresshandler);
            HttpResponseMessage response = null;

            try
            {
                response = await cl.SendAsync(msg, token);

                if (response.StatusCode == HttpStatusCode.Created)
                {
                    string dd = await response.Content.ReadAsStringAsync();

                    string          parentpath = string.Empty;
                    AmazonDirectory dir        = parent as AmazonDirectory;
                    if (dir == null || !dir.IsRoot)
                    {
                        parentpath = FullName;
                    }
                    AmazonFile file = new AmazonFile(parentpath, FS)
                    {
                        Parent = dir
                    };
                    file.SetData(dd);
                    progress.Report(new FileProgress
                    {
                        Percentage   = 100,
                        TotalSize    = file.Size,
                        TransferSize = file.Size
                    });
                    return(new FileSystemResult <IFile>(file));
                }
                return(new FileSystemResult <IFile>("Http Error : " + response.StatusCode));
            }
            catch (Exception e)
            {
                return(new FileSystemResult <IFile>("Exception Error : " + e.Message));
            }
            finally
            {
                response?.Dispose();
                cl?.Dispose();
                handler?.Dispose();
                progresshandler?.Dispose();
                msg?.Dispose();
            }
        }