Exemplo n.º 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.");
            }
        }
Exemplo n.º 2
0
 public void RenameItem(WsItem item, string newName)
 {
     if (item is WsFile file)
     {
         (FindFolderItemsResolver(file.PathInfo.Folder) as FolderItemsResolver)?.Rename(file, newName);
     }
     else if (item is WsFolder folder)
     {
         MoveFolder(folder.PathInfo.Folder, new WsFolderPath(folder.PathInfo.Parent.FullPath + newName, folder.PathInfo.IsPrivate));
     }
 }
Exemplo n.º 3
0
 public static FindData ConvertToFindData(this WsItem item)
 {
     if (item is WsFolder folderInfo)
     {
         return(new FindData(folderInfo.PathInfo.Name, 0, FileAttributes.Directory, folderInfo.Created));
     }
     if (item is WsFile fileInfo)
     {
         return(new FindData(fileInfo.PathInfo.Name, (ulong)fileInfo.Size, FileAttributes.Normal, fileInfo.Created));
     }
     throw new ArgumentException($"Not supported {typeof(WsItem).Name} of type {item.GetType().Name}");
 }
Exemplo n.º 4
0
        internal async Task MoveItem(WsItem sourceItem, WsFolder targetFolder)
        {
            if (sourceItem == null)
            {
                throw new ArgumentNullException(nameof(sourceItem));
            }
            if (targetFolder == null)
            {
                throw new ArgumentNullException(nameof(targetFolder));
            }
            if (sourceItem.PathInfoGeneric.Folder.Equals(targetFolder.PathInfo))
            {
                throw new ArgumentException($"{nameof(sourceItem)} and {nameof(targetFolder)} are equal folder.");
            }
            CheckConnected();

            Task <Result> MoveFunc()
            {
                FormUrlEncodedContent formContent = CreateFormContent(new[]
                {
                    new KeyValuePair <string, string>("src", sourceItem.PathInfoGeneric.FullPath),
                    new KeyValuePair <string, string>("src_private", sourceItem.PathInfoGeneric.IsPrivate ? "1" : "0"),
                    new KeyValuePair <string, string>("dest", targetFolder.PathInfo.FullPath),
                    new KeyValuePair <string, string>("dest_private", targetFolder.PathInfo.IsPrivate ? "1" : "0"),
                });

                return(_httpClient.PostFormData <Result>(API_MOVE_FILE_URI, formContent));
            }

            if (sourceItem is WsFile file)
            {
                WsFile targetFile = await FindFile(new WsFilePath(targetFolder.PathInfo, file.PathInfo.Name));

                if (targetFile != null)
                {
                    await targetFile.Delete();
                }
                await PostFormDataWithNotFoundAndLoginRetry(file, MoveFunc);
            }
            else
            {
                await PostFormDataWithLoginRetry(MoveFunc);
            }

            _createdFileResolver.MoveItem(sourceItem, targetFolder);
            sourceItem.ChangePathInInstance(targetFolder.PathInfo.FullPath + sourceItem.PathInfoGeneric.Name, targetFolder.PathInfo.IsPrivate);
        }
Exemplo n.º 5
0
 public void RemoveItem(WsItem item)
 {
     if (item is WsFile file)
     {
         FindFolderItemsResolver(file.PathInfo.Folder)?.RemoveFileAndFolderIfEmpty(file);
     }
     else if (item is WsFolder folder)
     {
         string folderPath = folder.PathInfo.Folder.FullPath;
         KeyValuePair <WsFolderPath, FolderItemsResolver>[] removingFolders = _dic.Where(e => e.Key.IsPrivate == folder.PathInfo.IsPrivate && e.Key.FullPath.StartsWith(folderPath)).ToArray();
         foreach (KeyValuePair <WsFolderPath, FolderItemsResolver> removingFolder in removingFolders)
         {
             RemoveFolder(removingFolder.Key);
             _ = removingFolder.Value.Clear(); // no wait for ending
         }
     }
 }
Exemplo n.º 6
0
        public void MoveItem(WsItem item, WsFolder targetFolder)
        {
            FolderItemsResolver sourceFolderItemsResolver = (FindFolderItemsResolver(item.PathInfoGeneric.Folder) as FolderItemsResolver);

            if (sourceFolderItemsResolver == null)
            {
                return;
            }
            if (item is WsFile file)
            {
                sourceFolderItemsResolver.RemoveFileAndFolderIfEmpty(file);
                if (file.IsReady == false)
                {
                    Add(targetFolder.PathInfo, file.PathInfo.Name, file.Ident, file.Size);
                }
            }
            else if (item is WsFolder folder)
            {
                MoveFolder(folder.PathInfo, targetFolder.PathInfo);
            }
        }
Exemplo n.º 7
0
        internal async Task DeleteItem(WsItem item)
        {
            CheckConnected();

            Task <Result> DeleteFunc()
            {
                FormUrlEncodedContent formContent = this.CreateFormContent(new[] { new KeyValuePair <string, string>("ident", item.Ident) });

                return(_httpClient.PostFormData <Result>(API_REMOVE_FILE_URI, formContent));
            }

            if (item is WsFile file)
            {
                await PostFormDataWithNotFoundAndLoginRetry(file, DeleteFunc);
            }
            else
            {
                await PostFormDataWithLoginRetry(DeleteFunc);
            }
            _createdFileResolver.RemoveItem(item);
        }
Exemplo n.º 8
0
        internal async Task RenameItem(WsItem item, string newName)
        {
            if (string.IsNullOrWhiteSpace(newName))
            {
                throw new ArgumentNullException(nameof(newName));
            }
            CheckConnected();

            Task <Result> RenameFunc()
            {
                FormUrlEncodedContent formContent = CreateFormContent(new[]
                {
                    new KeyValuePair <string, string>("ident", item.Ident),
                    new KeyValuePair <string, string>("name", newName)
                });

                return(_httpClient.PostFormData <Result>(API_RENAME_FILE_URI, formContent));
            }

            if (item is WsFile file)
            {
                WsFile targetFile = await FindFile(new WsFilePath(file.PathInfo.Folder, newName));

                if (targetFile != null)
                {
                    await targetFile.Delete();
                }
                await PostFormDataWithNotFoundAndLoginRetry(file, RenameFunc);
            }
            else
            {
                await PostFormDataWithLoginRetry(RenameFunc);
            }

            _createdFileResolver.RenameItem(item, newName);
            item.ChangeNameInInstance(newName);
        }