コード例 #1
0
        internal async Task <WsItem> CopyItem(WsItem sourceItem, WsFolder targetFolder, CancellationToken cancellationToken, IProgress <int> progress)
        {
            if (sourceItem == null)
            {
                throw new ArgumentNullException(nameof(sourceItem));
            }
            if (targetFolder == null)
            {
                throw new ArgumentNullException(nameof(targetFolder));
            }
            if (cancellationToken == null)
            {
                throw new ArgumentNullException(nameof(cancellationToken));
            }

            progress?.Report(0);

            if (sourceItem is WsFile sourceFile)
            {
                using (HttpResponseMessage response = await DownloadFile(sourceFile, cancellationToken))
                {
                    long       fileSize       = response.Content.Headers.ContentLength ?? sourceFile.Size;
                    int        bufferSize     = CalculateBuffer(fileSize);
                    WsFilePath targetFilePath = new WsFilePath(targetFolder.PathInfo, sourceItem.PathInfoGeneric.Name);
                    using (TransportStream sourceTrasportStream = new TransportStream(await response.Content.ReadAsStreamAsync(), fileSize, null))
                    {
                        return(await UploadFile(sourceTrasportStream, fileSize, targetFilePath, cancellationToken, progress));
                    }
                }
            }
            else
            {
                throw new NotSupportedException("Copy folder not supported.");
            }
        }
コード例 #2
0
 public async Task <WsFile> FindFile(WsFilePath filePath)
 {
     using (WsItemsReaderEngine filesResult = await GetItem(filePath, true))
     {
         return(filesResult.GetItems().OfType <WsFile>().Where(f => f.PathInfo.Equals(filePath)).SingleOrDefault());
     };
 }
コード例 #3
0
        public async Task <FileSystemExitCode> UploadFile(FileInfo sourceFileName, WsPath targetFileName, bool overwrite, IProgress <int> progress, CancellationToken cancellationToken)
        {
            try
            {
                await EnsureLogin();

                bool       uploadDirect   = overwrite;
                WsFilePath targetFilePath = targetFileName.GetFilePath();
                if (overwrite == false)
                {
                    WsFile file = await _apiClient.FindFile(targetFilePath);

                    if (file != null)
                    {
                        if (overwrite == false)
                        {
                            return(FileSystemExitCode.FileExists);
                        }
                        using (FileStream sourceStream = sourceFileName.OpenRead())
                        {
                            await file.Replace(sourceStream, cancellationToken, progress);
                        }
                    }
                    else
                    {
                        uploadDirect = true;
                    }
                }
                if (uploadDirect)
                {
                    using (FileStream sourceStream = sourceFileName.OpenRead())
                    {
                        await _apiClient.UploadFile(sourceStream, sourceStream.Length, targetFilePath, cancellationToken, progress);
                    }
                }
                return(FileSystemExitCode.OK);
            }
            catch (TaskCanceledException)
            {
                return(FileSystemExitCode.UserAbort);
            }
            catch (FileNotFoundException)
            {
                return(FileSystemExitCode.FileNotFound);
            }
            // TODO: dialog for file password, Exception throw in WsApiClient.CheckResultStatus
            //catch (??)
            //{
            //    return ??;
            //}
            catch (Exception ex)
            {
                if (ShowError("Upload file error", ex, true) == false)
                {
                    return(FileSystemExitCode.UserAbort);
                }
                return(FileSystemExitCode.ReadError);
            }
        }
コード例 #4
0
        public async Task <WsFile> GetFile(WsFilePath filePath)
        {
            WsFile file = await FindFile(filePath);

            if (file == null)
            {
                throw new FileNotFoundException("Required file not found on server.", filePath.ToString());
            }
            return(file);
        }
コード例 #5
0
 internal Task <WsItemsReaderEngine> GetItem(WsFilePath filePath, bool useCreatedFileResolver)
 {
     return(GetItemsOrItem(filePath, true, useCreatedFileResolver));
 }
コード例 #6
0
        internal async Task <WsFile> UploadFile(Stream sourceStream, long sourceSize, WsFilePath targetFilePath, bool ensureDeleteIfNeed, CancellationToken cancellationToken, IProgress <int> progress)
        {
            if (sourceStream == null)
            {
                throw new ArgumentNullException(nameof(sourceStream));
            }
            if (sourceStream.CanRead == false)
            {
                throw new NotSupportedException("The source stream does not support reading.");
            }
            if (cancellationToken == null)
            {
                throw new ArgumentNullException(nameof(cancellationToken));
            }
            CheckConnected();
            progress?.Report(0);

            if (ensureDeleteIfNeed)
            {
                WsFile file = await FindFile(targetFilePath);

                if (file != null)
                {
                    await file.Delete();
                }
            }

            MultipartFormDataContent form = new MultipartFormDataContent();

            form.Add(new StringContent(WebUtility.UrlEncode(targetFilePath.Name)), "name");
            form.Add(new StringContent(_loginInfo.LoginToken), "wst");
            form.Add(new StringContent(WebUtility.UrlEncode(targetFilePath.Folder.FullPath)), "folder");
            form.Add(new StringContent(targetFilePath.IsPrivate ? "1" : "0"), "private");
            form.Add(new StringContent("0"), "adult");
            form.Add(new StringContent(sourceSize.ToString()), "total");
            //form.Add(new StringContent(""), "offset");

            TransportStream        sourceTransportStream = new TransportStream(sourceStream, sourceSize, progress);
            TransportStreamContent fileContent           = new TransportStreamContent(sourceTransportStream, targetFilePath.Name, CalculateBuffer(sourceSize));

            form.Add(fileContent);

            UploadResult uploadResult = await _httpClient.PostFormData <UploadResult>(await GetUploadFileUrl(), form, cancellationToken, false, TimeSpan.FromHours(24));

            if (string.IsNullOrWhiteSpace(uploadResult?.Ident))
            {
                throw new IOException($"Server upload error: result={uploadResult.Result}");
            }

            return(_createdFileResolver.Add(targetFilePath.Folder, targetFilePath.Name, uploadResult.Ident, sourceSize));
        }
コード例 #7
0
 public Task <WsFile> UploadFile(Stream sourceStream, long sourceSize, WsFilePath targetFilePath, CancellationToken cancellationToken, IProgress <int> progress = null)
 {
     return(UploadFile(sourceStream, sourceSize, targetFilePath, true, cancellationToken, progress));
 }