private bool _hashDownloadFile(AttachmentMold attachment) { var fileInfo = new FileInfo(attachment.FileName); var result = fileInfo.Exists; return(result); }
public AttachmentMessageViewModel(AttachmentMold attachment) { var mainWindowViewModel = Locator.Current.GetService <MainWindowViewModel>(); _isDownload = mainWindowViewModel.IsExistAttachment(attachment); Id = attachment.Id; SenderId = attachment.SenderId; FileName = attachment.FileName; Size = attachment.Size; UploadDateTime = attachment.UploadDateTime; Hash = attachment.Hash; Text = !_isDownload ? "Загрузить" : "Открыть"; DownloadCommand = ReactiveCommand.Create <AttachmentMold>(async attachment => { if (!_isDownload) { _isDownload = await mainWindowViewModel.DownloadAttachment(attachment); Text = !_isDownload ? "Загрузить" : "Открыть"; } else { mainWindowViewModel.OpenAttachment(attachment.FileName); } }); }
public async Task <bool> DownloadAttachment(AttachmentMold data) { try { //Тут надо убрать префикс получаемого файла var pref = "attachment/"; var attachment = await serviceClient.GetAsync(new GetAttachment { Id = data.Id.Replace(pref, string.Empty) }); if (attachment == null) { return(false); } var savePath = Path.Combine(settings?.AttachmentDefaultPath, data.FileName); var saveFileInfo = new FileInfo(savePath); if (!saveFileInfo.Directory.Exists) { saveFileInfo.Directory.Create(); } if (!string.IsNullOrEmpty(savePath)) { await using (var fileStream = File.Create(savePath, (int)attachment.Length)) { const int bufferSize = 4194304; var buffer = new byte[bufferSize]; attachment.Seek(0, SeekOrigin.Begin); while (attachment.Position < attachment.Length) { var read = await attachment.ReadAsync(buffer, 0, bufferSize); await fileStream.WriteAsync(buffer, 0, read); } await fileStream.FlushAsync(); } } return(true); } catch (Exception e) { //TODO вывести ошибку в будущем return(false); } }
public bool IsExistAttachment(AttachmentMold data) { var fileInfo = new FileInfo(Path.Combine(settings?.AttachmentDefaultPath, data.FileName)); return(fileInfo.Exists && fileInfo.Length == data.Size); }