Exemplo n.º 1
0
        public async Task <StoreItemResult> CopyAsync(IStoreCollection destination, string name, bool overwrite, IHttpContext httpContext)
        {
            try
            {
                // If the destination is also a disk-store, then we can use the FileCopy API
                // (it's probably a bit more efficient than copying in C#)
                var diskCollection = destination as DiskStoreCollection;
                if (diskCollection != null)
                {
                    // Check if the collection is writable
                    if (!diskCollection.IsWritable)
                    {
                        return(new StoreItemResult(DavStatusCode.PreconditionFailed));
                    }

                    var destinationPath = Path.Combine(diskCollection.FullPath, name);

                    // Check if the file already exists
                    var fileExists = File.Exists(destinationPath);
                    if (fileExists && !overwrite)
                    {
                        return(new StoreItemResult(DavStatusCode.PreconditionFailed));
                    }

                    // Copy the file
                    File.Copy(_fileInfo.FullName, destinationPath, true);

                    // Return the appropriate status
                    return(new StoreItemResult(fileExists ? DavStatusCode.NoContent : DavStatusCode.Created));
                }
                else
                {
                    // Create the item in the destination collection
                    var result = await destination.CreateItemAsync(name, overwrite, httpContext).ConfigureAwait(false);

                    // Check if the item could be created
                    if (result.Item != null)
                    {
                        using (var sourceStream = await GetReadableStreamAsync(httpContext).ConfigureAwait(false))
                        {
                            var copyResult = await result.Item.UploadFromStreamAsync(httpContext, sourceStream).ConfigureAwait(false);

                            if (copyResult != DavStatusCode.Ok)
                            {
                                return(new StoreItemResult(copyResult, result.Item));
                            }
                        }
                    }

                    // Return result
                    return(new StoreItemResult(result.Result, result.Item));
                }
            }
            catch (Exception exc)
            {
                s_log.Log(LogLevel.Error, () => "Unexpected exception while copying data.", exc);
                return(new StoreItemResult(DavStatusCode.InternalServerError));
            }
        }
        public async Task <StoreItemResult> CopyAsync(IStoreCollection destination, string name, bool overwrite, IHttpContext httpContext)
        {
            try
            {
                var collection = destination as MailruStoreCollection;
                if (collection != null)
                {
                    if (!collection.IsWritable)
                    {
                        return(new StoreItemResult(DavStatusCode.PreconditionFailed));
                    }

                    var destinationPath = WebDavPath.Combine(collection.FullPath, name);

                    // check if the file already exists??

                    await Cloud.Instance(httpContext).Copy(_fileInfo, destinationPath);

                    return(new StoreItemResult(DavStatusCode.Created));
                }


                // Create the item in the destination collection
                var result = await destination.CreateItemAsync(name, overwrite, httpContext).ConfigureAwait(false);

                if (result.Item != null)
                {
                    using (var sourceStream = await GetReadableStreamAsync(httpContext).ConfigureAwait(false))
                    {
                        var copyResult = await result.Item.UploadFromStreamAsync(httpContext, sourceStream).ConfigureAwait(false);

                        if (copyResult != DavStatusCode.Ok)
                        {
                            return(new StoreItemResult(copyResult, result.Item));
                        }
                    }
                }

                return(new StoreItemResult(result.Result, result.Item));
            }
            catch (IOException ioException) when(ioException.IsDiskFull())
            {
                SLog.Log(LogLevel.Error, () => "Out of disk space while copying data.", ioException);
                return(new StoreItemResult(DavStatusCode.InsufficientStorage));
            }
            catch (Exception exc)
            {
                SLog.Log(LogLevel.Error, () => "Unexpected exception while copying data.", exc);
                return(new StoreItemResult(DavStatusCode.InternalServerError));
            }
        }