public async Task <Stream> Create(File file, FileUploadedDelegate onUploaded = null, bool discardEncryption = false)
        {
            if (!(await _cloud.GetItemAsync(file.Path, MailRuCloud.ItemType.Folder) is Folder folder))
            {
                throw new DirectoryNotFoundException(file.Path);
            }

            Stream stream;

            bool cryptRequired = _cloud.IsFileExists(CryptFileInfo.FileName, WebDavPath.GetParents(folder.FullPath).ToList()).Any();

            if (cryptRequired && !discardEncryption)
            {
                if (!_cloud.Account.Credentials.CanCrypt)
                {
                    throw new Exception($"Cannot upload {file.FullPath} to crypt folder without additional password!");
                }

                // #142 remove crypted file parts if size changed
                var remoteFile = folder.Files.FirstOrDefault(f => f.FullPath == file.FullPath);
                if (remoteFile != null)
                {
                    await _cloud.Remove(remoteFile);
                }

                stream = GetCryptoStream(file, onUploaded);
            }
            else
            {
                stream = GetPlainStream(file, onUploaded);
            }

            return(stream);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Убрать все привязки на мёртвые ссылки
        /// </summary>
        /// <param name="doWriteHistory"></param>
        public async Task <int> RemoveDeadLinks(bool doWriteHistory)
        {
            var removes = _itemList.Items
                          .AsParallel()
                          .WithDegreeOfParallelism(5)
                          .Select(it => GetItemLink(WebDavPath.Combine(it.MapTo, it.Name)).Result)
                          .Where(itl =>
                                 itl.IsBad ||
                                 _cloud.GetItemAsync(itl.MapPath, MailRuCloud.ItemType.Folder, false).Result == null)
                          .ToList();

            if (removes.Count == 0)
            {
                return(0);
            }

            _itemList.Items.RemoveAll(it => removes.Any(rem => WebDavPath.PathEquals(rem.MapPath, it.MapTo) && rem.Name == it.Name));

            if (removes.Any())
            {
                if (doWriteHistory)
                {
                    foreach (var link in removes)
                    {
                        _itemCache.Invalidate(link.FullPath, link.MapPath);
                    }

                    string path = WebDavPath.Combine(WebDavPath.Root, HistoryContainerName);
                    string res  = await _cloud.DownloadFileAsString(path);

                    var history = new StringBuilder(res ?? string.Empty);
                    foreach (var link in removes)
                    {
                        history.Append($"{DateTime.Now} REMOVE: {link.Href} {link.Name}\r\n");
                    }
                    _cloud.UploadFile(path, history.ToString());
                }
                Save();
                return(removes.Count);
            }

            return(0);
        }