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);
 }
Example #2
0
        //const string ContentCompression = "gzip";

        /// <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="writeOptions">The write options.</param>
        public long Write(Action <Stream> writer, StreamingCondition condition, StreamingWriteOptions writeOptions)
        {
            try
            {
                var mapped = Map(condition);

                return(BlobStorageUtil.Write(mapped, _blob, writer, writeOptions));
            }
            catch (StorageServerException ex)
            {
                switch (ex.ErrorCode)
                {
                case StorageErrorCode.ServiceIntegrityCheckFailed:
                    throw StreamingErrors.IntegrityFailure(this, ex);

                default:
                    throw;
                }
            }
            catch (StorageClientException ex)
            {
                switch (ex.ErrorCode)
                {
                case StorageErrorCode.ConditionFailed:
                    throw StreamingErrors.ConditionFailed(this, condition, ex);

                case StorageErrorCode.ContainerNotFound:
                    throw StreamingErrors.ContainerNotFound(this, ex);

                default:
                    throw;
                }
            }
        }
Example #3
0
        public Optional <StreamingItemInfo> GetInfo(StreamingCondition condition)
        {
            try
            {
                _blob.FetchAttributes(Map(condition));
                return(BlobStorageUtil.MapFetchedAttrbitues(_blob));
            }
            catch (StorageClientException e)
            {
                switch (e.ErrorCode)
                {
                case StorageErrorCode.ContainerNotFound:
                case StorageErrorCode.ResourceNotFound:
                case StorageErrorCode.BlobNotFound:
                case StorageErrorCode.ConditionFailed:
                    return(Optional <StreamingItemInfo> .Empty);

                case StorageErrorCode.BadRequest:
                    switch (e.StatusCode)
                    {
                    case HttpStatusCode.PreconditionFailed:
                        return(Optional <StreamingItemInfo> .Empty);

                    default:
                        throw;
                    }
                }
                throw;
            }
        }
        /// <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);
            }
        }
 void ThrowIfConditionFailed(StreamingCondition condition)
 {
     if (!Satisfy(condition))
     {
         throw StreamingErrors.ConditionFailed(this, condition);
     }
 }
        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));
        }
        bool Satisfy(StreamingCondition condition)
        {
            var info = GetUnconditionalInfo();

            return(info
                   .Convert(s => new LocalStreamingInfo(s.ETag))
                   .Convert(s => condition.Satisfy(s), () => condition.Satisfy()));
        }
        /// <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);
        }
        /// <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();
            }
        }
Example #10
0
        static BlobRequestOptions Map(StreamingCondition condition,
                                      StreamingCondition copySourceAccessCondition = default(StreamingCondition))
        {
            if ((condition.Type == StreamingConditionType.None) &&
                (copySourceAccessCondition.Type == StreamingConditionType.None))
            {
                return(null);
            }

            return(new BlobRequestOptions
            {
                AccessCondition = MapCondition(condition),
                CopySourceAccessCondition = MapCondition(copySourceAccessCondition)
            });
        }
        /// <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 = _file.OpenRead())
            {
                reader(props, read);
            }
        }
        /// <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 = _file.OpenWrite())
            {
                writer(file);
                // stream will probably be closed here.
            }
            Refresh();
            return(_file.Length);
        }
Example #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)
        {
            try
            {
                var mapped = Map(condition);
                BlobStorageUtil.Read(mapped, _blob, reader);
            }
            catch (StreamingItemIntegrityException e)
            {
                throw StreamingErrors.IntegrityFailure(this, e);
            }
            catch (StorageClientException e)
            {
                switch (e.ErrorCode)
                {
                case StorageErrorCode.ContainerNotFound:
                    throw StreamingErrors.ContainerNotFound(this, e);

                case StorageErrorCode.ResourceNotFound:
                case StorageErrorCode.BlobNotFound:
                    throw StreamingErrors.ItemNotFound(this, e);

                case StorageErrorCode.ConditionFailed:
                    throw StreamingErrors.ConditionFailed(this, condition, e);

                case StorageErrorCode.ServiceIntegrityCheckFailed:
                    throw StreamingErrors.IntegrityFailure(this, e);

                case StorageErrorCode.BadRequest:
                    switch (e.StatusCode)
                    {
                    // for some reason Azure Storage happens to get here as well
                    case HttpStatusCode.PreconditionFailed:
                    case HttpStatusCode.NotModified:
                        throw StreamingErrors.ConditionFailed(this, condition, e);

                    default:
                        throw;
                    }

                default:
                    throw;
                }
            }
        }
Example #14
0
        static AccessCondition MapCondition(StreamingCondition condition)
        {
            switch (condition.Type)
            {
            case StreamingConditionType.None:
                return(AccessCondition.None);

            case StreamingConditionType.IfMatch:
                var x = ExposeException(condition.ETag, "'ETag' should be present");
                return(AccessCondition.IfMatch(x));

            case StreamingConditionType.IfNoneMatch:
                var etag = ExposeException(condition.ETag, "'ETag' should be present");
                return(AccessCondition.IfNoneMatch(etag));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Example #15
0
        /// <summary>
        /// Removes the item, ensuring that the specified condition is met.
        /// </summary>
        /// <param name="condition">The condition.</param>
        public void Delete(StreamingCondition condition)
        {
            try
            {
                var options = Map(condition);
                _blob.Delete(options);
            }
            catch (StorageClientException ex)
            {
                switch (ex.ErrorCode)
                {
                case StorageErrorCode.ContainerNotFound:
                    throw StreamingErrors.ContainerNotFound(this, ex);

                case StorageErrorCode.BlobNotFound:
                case StorageErrorCode.ConditionFailed:
                    return;

                default:
                    throw;
                }
            }
        }
Example #16
0
        public void CopyFrom(IStreamingItem sourceItem,
                             StreamingCondition condition,
                             StreamingCondition copySourceCondition,
                             StreamingWriteOptions writeOptions)
        {
            var item = sourceItem as BlobStreamingItem;

            if (item != null)
            {
                try
                {
                    _blob.CopyFromBlob(item._blob, Map(condition, copySourceCondition));
                }
                catch (StorageClientException e)
                {
                    switch (e.ErrorCode)
                    {
                    case StorageErrorCode.BlobNotFound:
                        throw StreamingErrors.ItemNotFound(this, e);

                    default:
                        throw;
                    }
                }
            }
            else
            {
                // based on the default write block size of BLOB
                const int bufferSize = 0x400000;
                Write(
                    targetStream =>
                    sourceItem.ReadInto(
                        (props, stream) => stream.CopyTo(targetStream, bufferSize),
                        copySourceCondition), condition,
                    writeOptions);
            }
        }