public AttachmentFileInfo(ISDataClient client, bool formMode, IDirectoryInfo directory, Attachment attachment)
     : base(directory)
 {
     _client = client;
     _formMode = formMode;
     _attachment = attachment;
 }
        public IFileInfo GetFileInfo(string path)
        {
            var dirPath = Path.GetDirectoryName(path);
            var fileName = Path.GetFileName(path);
            var dir = GetDirectoryInfo(dirPath);
            var file = dir.GetFiles().FirstOrDefault(item => string.Equals(fileName, item.Name, StringComparison.OrdinalIgnoreCase));

            if (file == null)
            {
                if (path.StartsWith("sdata:\\attachments", StringComparison.OrdinalIgnoreCase))
                {
                    var attachment = new Attachment {FileName = fileName};
                    file = new AttachmentFileInfo(_client, _formMode, dir, attachment);
                }
                else
                {
                    var document = new LibraryDocument
                        {
                            FileName = fileName,
                            Directory = new SDataResource {Key = ((LibraryDirectory) ((IResourceHolder) dir).Resource).Key}
                        };
                    file = new LibraryFileInfo(_client, _formMode, dir, document);
                }
            }

            return file;
        }
        public override void Save(Stream stream)
        {
            if (stream == null)
            {
                _attachment = string.IsNullOrEmpty(_attachment.Key)
                    ? _client.Post(_attachment)
                    : _client.Put(_attachment);
                return;
            }

            var parms = new SDataParameters
                {
                    Files = {new AttachedFile(null, _attachment.FileName, stream)}
                };

            if (string.IsNullOrEmpty(_attachment.Key))
            {
                parms.Method = HttpMethod.Post;
                parms.Path = "attachments";
            }
            else
            {
                parms.Method = HttpMethod.Put;
                parms.Path = "attachments(" + SDataUri.FormatConstant(_attachment.Key) + ")";
                parms.ETag = _attachment.ETag;
            }

            if (_formMode)
            {
                foreach (var prop in typeof (Attachment).GetProperties())
                {
                    var name = _client.NamingScheme.GetName(prop);
                    if (!name.StartsWith("$", StringComparison.Ordinal) && !new[] {"physicalFileName", "fileExists", "fileSize", "createDate", "createUser", "modifyDate", "modifyUser"}.Contains(name, StringComparer.Ordinal))
                    {
                        var value = prop.GetValue(_attachment, null);
                        if (value != null)
                        {
                            parms.Form[name] = value.ToString();
                        }
                    }
                }

                parms.Path += "/file";
                var results = _client.Execute(parms);
                if (!string.IsNullOrEmpty(results.Location))
                {
                    var selector = new SDataUri(results.Location).GetPathSegment(4).Selector;
                    _attachment.Key = selector.Substring(1, selector.Length - 2);
                    _attachment.ETag = results.ETag;
                }
            }
            else
            {
                parms.Content = _attachment;
                _attachment = _client.Execute<Attachment>(parms).Content;
            }
        }
 public override void Refresh()
 {
     _attachment = _client.Get<Attachment>(_attachment.Key);
 }