Пример #1
0
        /// <summary>
        /// Creates this storage item from another.
        /// </summary>
        /// <param name="sourceItem">The target.</param>
        /// <param name="condition">The condition.</param>
        /// <param name="copySourceCondition">The copy source condition.</param>
        /// <param name="options">The options.</param>
        /// <exception cref="StreamingItemNotFoundException">when source storage is not found</exception>
        /// <exception cref="StreamingItemIntegrityException">when integrity check fails</exception>
        public void CopyFrom(IStreamingItem sourceItem, StreamingCondition condition, StreamingCondition copySourceCondition,
                             StreamingWriteOptions options)
        {
            var item = sourceItem as FileStreamingItem;

            if (item != null)
            {
                Refresh();
                ThrowIfContainerNotFound();
                ThrowIfConditionFailed(condition);

                item.Refresh();
                item.ThrowIfContainerNotFound();
                item.ThrowIfItemNotFound();
                item.ThrowIfConditionFailed(copySourceCondition);

                item._file.CopyTo(_file.FullName, true);
            }
            else
            {
                const int bufferSize = 64 * 1024;
                Write(
                    targetStream =>
                    sourceItem.ReadInto((props, stream) => stream.CopyTo(targetStream, bufferSize),
                                        copySourceCondition), condition, options);
            }
        }
Пример #2
0
 void ThrowIfConditionFailed(StreamingCondition condition)
 {
     if (!Satisfy(condition))
     {
         throw StreamingErrors.ConditionFailed(this, condition);
     }
 }
Пример #3
0
        /// <summary>
        /// Creates this storage item from another.
        /// </summary>
        /// <param name="sourceItem">The target.</param>
        /// <param name="condition">The condition.</param>
        /// <param name="copySourceCondition">The copy source condition.</param>
        /// <param name="options">The options.</param>
        /// <exception cref="StreamingItemNotFoundException">when source storage is not found</exception>
        /// <exception cref="StreamingItemIntegrityException">when integrity check fails</exception>
        public void CopyFrom(IStreamingItem sourceItem, StreamingCondition condition, StreamingCondition copySourceCondition,
            StreamingWriteOptions options)
        {
            var item = sourceItem as FileStreamingItem;

            if (item != null)
            {
                Refresh();
                ThrowIfContainerNotFound();
                ThrowIfConditionFailed(condition);

                item.Refresh();
                item.ThrowIfContainerNotFound();
                item.ThrowIfItemNotFound();
                item.ThrowIfConditionFailed(copySourceCondition);

                item._file.CopyTo(_file.FullName, true);
            }
            else
            {
                const int bufferSize = 64*1024;
                Write(
                    targetStream =>
                        sourceItem.ReadInto((props, stream) => stream.CopyTo(targetStream, bufferSize),
                            copySourceCondition), condition, options);
            }
        }
Пример #4
0
        public static Exception ConditionFailed(IStreamingItem item, StreamingCondition condition, Exception inner = null)
        {
            var message = string.Format(CultureInfo.InvariantCulture, "Storage condition '{0}' failed for '{1}'",
                                        condition,
                                        item.FullPath);

            return(new StreamingConditionFailedException(message, inner));
        }
Пример #5
0
 bool Satisfy(StreamingCondition condition)
 {
     return
         (GetUnconditionalInfo().Convert(
              s => new LocalStreamingInfo(s.ETag)).Convert(
              s => condition.Satisfy(new[] { s }),
              () => condition.Satisfy(new LocalStreamingInfo[0])));
 }
Пример #6
0
        bool Satisfy(StreamingCondition condition)
        {
            var info = GetUnconditionalInfo();

            return(info
                   .Convert(s => new LocalStreamingInfo(s.ETag))
                   .Convert(s => condition.Satisfy(s), () => condition.Satisfy()));
        }
Пример #7
0
        /// <summary>
        /// Gets the info about this item. It returns empty result if the item does not exist or does not match the condition
        /// </summary>
        /// <param name="condition">The condition.</param>
        /// <returns></returns>
        public Optional <StreamingItemInfo> GetInfo(StreamingCondition condition = new StreamingCondition())
        {
            if (_parent.Contains(this) && Satisfy(condition))
            {
                return(GetUnconditionalInfo());
            }

            return(Optional <StreamingItemInfo> .Empty);
        }
Пример #8
0
        /// <summary>
        /// Removes the item, ensuring that the specified condition is met.
        /// </summary>
        /// <param name="condition">The condition.</param>
        public void Delete(StreamingCondition condition = new StreamingCondition())
        {
            ThrowIfContainerNotFound();
            if (!_parent.Contains(this) || !Satisfy(condition))
            {
                return;
            }

            _parent.Remove(this);
        }
Пример #9
0
        /// <summary>
        /// Attempts to read the storage item.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="condition">The condition.</param>
        /// <exception cref="StreamingItemNotFoundException">if the item does not exist.</exception>
        /// <exception cref="StreamingContainerNotFoundException">if the container for the item does not exist</exception>
        /// <exception cref="StreamingItemIntegrityException">when integrity check fails</exception>
        public void ReadInto(ReaderDelegate reader, StreamingCondition condition = new StreamingCondition())
        {
            ThrowIfContainerNotFound();
            ThrowIfItemNotFound();
            ThrowIfConditionFailed(condition);

            var props = GetUnconditionalInfo().Value;

            using (var stream = new MemoryStream(_content))
                reader(props, stream);
        }
Пример #10
0
        /// <summary>
        /// Gets the info about this item. It returns empty result if the item does not exist or does not match the condition
        /// </summary>
        /// <param name="condition">The condition.</param>
        /// <returns></returns>
        public Optional <StreamingItemInfo> GetInfo(StreamingCondition condition)
        {
            Refresh();
            //ThrowIfContainerNotFound();

            if (_file.Exists && Satisfy(condition))
            {
                return(GetUnconditionalInfo());
            }
            return(Optional <StreamingItemInfo> .Empty);
        }
Пример #11
0
        /// <summary>
        /// Removes the item, ensuring that the specified condition is met.
        /// </summary>
        /// <param name="condition">The condition.</param>
        public void Delete(StreamingCondition condition)
        {
            Refresh();

            ThrowIfContainerNotFound();

            if (_file.Exists && Satisfy(condition))
            {
                _file.Delete();
            }
        }
Пример #12
0
        /// <summary>
        /// Performs the write operation, ensuring that the condition is met.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="condition">The condition.</param>
        /// <param name="options">The options.</param>
        /// <returns>number of bytes written</returns>
        /// <exception cref="StreamingItemIntegrityException">when integrity check fails during the upload</exception>
        public long Write(Action <Stream> writer, StreamingCondition condition = new StreamingCondition(), StreamingWriteOptions options = StreamingWriteOptions.None)
        {
            ThrowIfContainerNotFound();
            ThrowIfConditionFailed(condition);

            _parent.Add(this);

            using (var stream = new MemoryStream())
            {
                writer(stream);
                _content = stream.ToArray();
            }

            return(_content.Length);
        }
Пример #13
0
        /// <summary>
        /// Attempts to read the storage item.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="condition">The condition.</param>
        /// <exception cref="StreamingItemNotFoundException">if the item does not exist.</exception>
        /// <exception cref="StreamingContainerNotFoundException">if the container for the item does not exist</exception>
        /// <exception cref="StreamingItemIntegrityException">when integrity check fails</exception>
        public void ReadInto(ReaderDelegate reader, StreamingCondition condition)
        {
            Refresh();

            ThrowIfContainerNotFound();
            ThrowIfItemNotFound();
            ThrowIfConditionFailed(condition);

            var props = GetUnconditionalInfo().Value;

            using (var read = OpenForRead())
            {
                reader(props, read);
            }
        }
Пример #14
0
        /// <summary>
        /// Performs the write operation, ensuring that the condition is met.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="condition">The condition.</param>
        /// <param name="options">The options.</param>
        /// <returns>number of bytes written</returns>
        /// <exception cref="StreamingItemIntegrityException">when integrity check fails during the upload</exception>
        public long Write(Action <Stream> writer, StreamingCondition condition, StreamingWriteOptions options)
        {
            Refresh();

            ThrowIfContainerNotFound();
            ThrowIfConditionFailed(condition);

            using (var file = OpenForWrite())
            {
                writer(file);
                // stream will probably be closed here.
            }
            Refresh();
            return(_file.Length);
        }
Пример #15
0
        /// <summary>
        /// Creates this storage item from another.
        /// </summary>
        /// <param name="sourceItem">The target.</param>
        /// <param name="condition">The condition.</param>
        /// <param name="copySourceCondition">The copy source condition.</param>
        /// <param name="options">The options.</param>
        /// <exception cref="StreamingItemNotFoundException">when source storage is not found</exception>
        /// <exception cref="StreamingItemIntegrityException">when integrity check fails</exception>
        public void CopyFrom(IStreamingItem sourceItem, StreamingCondition condition = new StreamingCondition(), StreamingCondition copySourceCondition = new StreamingCondition(), StreamingWriteOptions options = StreamingWriteOptions.None)
        {
            var source = sourceItem as MemoryStreamingItem;
            if (source != null)
            {
                ThrowIfContainerNotFound();
                ThrowIfConditionFailed(condition);

                source.ThrowIfContainerNotFound();
                source.ThrowIfItemNotFound();
                source.ThrowIfConditionFailed(copySourceCondition);

                _content = source._content;
                _parent.Add(this);
            }
            else
                Write(targetStream => sourceItem.ReadInto((props, stream) => stream.CopyTo(targetStream, 65536), copySourceCondition), condition, options);
        }
Пример #16
0
        /// <summary>
        /// Creates this storage item from another.
        /// </summary>
        /// <param name="sourceItem">The target.</param>
        /// <param name="condition">The condition.</param>
        /// <param name="copySourceCondition">The copy source condition.</param>
        /// <param name="options">The options.</param>
        /// <exception cref="StreamingItemNotFoundException">when source storage is not found</exception>
        /// <exception cref="StreamingItemIntegrityException">when integrity check fails</exception>
        public void CopyFrom(IStreamingItem sourceItem, StreamingCondition condition = new StreamingCondition(), StreamingCondition copySourceCondition = new StreamingCondition(), StreamingWriteOptions options = StreamingWriteOptions.None)
        {
            var source = sourceItem as MemoryStreamingItem;

            if (source != null)
            {
                ThrowIfContainerNotFound();
                ThrowIfConditionFailed(condition);

                source.ThrowIfContainerNotFound();
                source.ThrowIfItemNotFound();
                source.ThrowIfConditionFailed(copySourceCondition);

                _content = source._content;
                _parent.Add(this);
            }
            else
            {
                Write(targetStream => sourceItem.ReadInto((props, stream) => stream.CopyTo(targetStream, 65536), copySourceCondition), condition, options);
            }
        }
Пример #17
0
 void ThrowIfConditionFailed(StreamingCondition condition)
 {
     if (!Satisfy(condition))
         throw StreamingErrors.ConditionFailed(this, condition);
 }
Пример #18
0
 bool Satisfy(StreamingCondition condition)
 {
     return
         GetUnconditionalInfo().Convert(
             s => new LocalStreamingInfo(s.ETag)).Convert(
                 s => condition.Satisfy(new[] { s }),
                 () => condition.Satisfy(new LocalStreamingInfo[0]));
 }
Пример #19
0
        /// <summary>
        /// Performs the write operation, ensuring that the condition is met.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="condition">The condition.</param>
        /// <param name="options">The options.</param>
        /// <returns>number of bytes written</returns>
        /// <exception cref="StreamingItemIntegrityException">when integrity check fails during the upload</exception>
        public long Write(Action<Stream> writer, StreamingCondition condition, StreamingWriteOptions options)
        {
            Refresh();

            ThrowIfContainerNotFound();
            ThrowIfConditionFailed(condition);

            using (var file = OpenForWrite())
            {
                writer(file);
                // stream will probably be closed here.
            }
            Refresh();
            return _file.Length;
        }
Пример #20
0
 bool Satisfy(StreamingCondition condition)
 {
     var info = GetUnconditionalInfo();
     return info
         .Convert(s => new LocalStreamingInfo(s.ETag))
         .Convert(s => condition.Satisfy(s), () => condition.Satisfy());
 }
Пример #21
0
        /// <summary>
        /// Gets the info about this item. It returns empty result if the item does not exist or does not match the condition
        /// </summary>
        /// <param name="condition">The condition.</param>
        /// <returns></returns>
        public Optional<StreamingItemInfo> GetInfo(StreamingCondition condition)
        {
            Refresh();
            //ThrowIfContainerNotFound();

            if (_file.Exists && Satisfy(condition))
                return GetUnconditionalInfo();
            return Optional<StreamingItemInfo>.Empty;
        }
Пример #22
0
        /// <summary>
        /// Attempts to read the storage item.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="condition">The condition.</param>
        /// <exception cref="StreamingItemNotFoundException">if the item does not exist.</exception>
        /// <exception cref="StreamingContainerNotFoundException">if the container for the item does not exist</exception>
        /// <exception cref="StreamingItemIntegrityException">when integrity check fails</exception>
        public void ReadInto(ReaderDelegate reader, StreamingCondition condition)
        {
            Refresh();

            ThrowIfContainerNotFound();
            ThrowIfItemNotFound();
            ThrowIfConditionFailed(condition);

            var props = GetUnconditionalInfo().Value;
            using (var read = OpenForRead())
            {
                reader(props, read);
            }
        }
Пример #23
0
        /// <summary>
        /// Removes the item, ensuring that the specified condition is met.
        /// </summary>
        /// <param name="condition">The condition.</param>
        public void Delete(StreamingCondition condition)
        {
            Refresh();

            ThrowIfContainerNotFound();

            if (_file.Exists && Satisfy(condition))
                _file.Delete();
        }
Пример #24
0
        /// <summary>
        /// Removes the item, ensuring that the specified condition is met.
        /// </summary>
        /// <param name="condition">The condition.</param>
        public void Delete(StreamingCondition condition = new StreamingCondition())
        {
            ThrowIfContainerNotFound();
            if (!_parent.Contains(this) || !Satisfy(condition))
                return;

            _parent.Remove(this);
        }
Пример #25
0
        /// <summary>
        /// Performs the write operation, ensuring that the condition is met.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="condition">The condition.</param>
        /// <param name="options">The options.</param>
        /// <returns>number of bytes written</returns>
        /// <exception cref="StreamingItemIntegrityException">when integrity check fails during the upload</exception>
        public long Write(Action<Stream> writer, StreamingCondition condition = new StreamingCondition(), StreamingWriteOptions options = StreamingWriteOptions.None)
        {
            ThrowIfContainerNotFound();
            ThrowIfConditionFailed(condition);

            _parent.Add(this);

            using (var stream = new MemoryStream())
            {
                writer(stream);
                _content = stream.ToArray();
            }

            return _content.Length;
        }
Пример #26
0
        /// <summary>
        /// Gets the info about this item. It returns empty result if the item does not exist or does not match the condition
        /// </summary>
        /// <param name="condition">The condition.</param>
        /// <returns></returns>
        public Optional<StreamingItemInfo> GetInfo(StreamingCondition condition = new StreamingCondition())
        {
            if (_parent.Contains(this) && Satisfy(condition))
                return GetUnconditionalInfo();

            return Optional<StreamingItemInfo>.Empty;
        }
Пример #27
0
        /// <summary>
        /// Attempts to read the storage item.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="condition">The condition.</param>
        /// <exception cref="StreamingItemNotFoundException">if the item does not exist.</exception>
        /// <exception cref="StreamingContainerNotFoundException">if the container for the item does not exist</exception>
        /// <exception cref="StreamingItemIntegrityException">when integrity check fails</exception>
        public void ReadInto(ReaderDelegate reader, StreamingCondition condition = new StreamingCondition())
        {
            ThrowIfContainerNotFound();
            ThrowIfItemNotFound();
            ThrowIfConditionFailed(condition);

            var props = GetUnconditionalInfo().Value;
            using (var stream = new MemoryStream(_content))
                reader(props, stream);
        }