/// <summary> /// Возвращает транспортный объект файла для передачи клиенту. /// </summary> /// <param name="file">Файл.</param> /// <param name="loadOptions">Опции загрузки.</param> /// <returns></returns> public static WcfFileInfo FromFile(IFile file, GetFileOptions loadOptions = null) { if (file == null) { throw new ArgumentNullException("file"); } WcfFileInfo wcfFile = new WcfFileInfo() { UniqueID = file.UniqueID, FolderUniqueID = file.Folder.UniqueID, VersionUniqueID = file.VersionUniqueID, Name = file.Name, TimeCreated = file.TimeCreated, TimeModified = file.TimeModified, FolderUrl = file.FolderUrl, Url = file.Url, Size = file.Size }; if (loadOptions != null && loadOptions.LoadContent) { if (file.Content != null) { wcfFile.Content = file.Content; } else { //в буферном режиме не может быть тастолько большим //проверки на размер есть в вызывающем коде, до физического чтения содержимого int bufferedFileSize = (int)file.Size; wcfFile.Content = new byte[bufferedFileSize]; using (Stream st = file.Open()) { int read = st.Read(wcfFile.Content, 0, bufferedFileSize); if (read != file.Size) { throw new Exception(string.Format("Не удалось полностью считать содержимое файла.")); } } } } return(wcfFile); }
/// <summary> /// Обновляет содержимое файла в хранилище. /// </summary> /// <param name="folderUrl">Адрес папки.</param> /// <param name="fileUniqueID">Уникальный идентификатор файла.</param> /// <param name="content">Содержимое файла.</param> /// <param name="fileName">Имя файла.</param> /// <returns></returns> public WcfFileInfo UpdateFile(string folderUrl, Guid fileUniqueID, byte[] content, string fileName = null) { WcfFileInfo wcfFile = null; try { this.Authorize(); if (string.IsNullOrEmpty(folderUrl)) { throw new ArgumentNullException("folderUrl"); } if (fileUniqueID == Guid.Empty) { throw new ArgumentNullException("fileUniqueID"); } if (content == null || content.Length == 0) { throw new ArgumentNullException("content"); } this.Logger.WriteFormatMessage("UpdateFile:Начало обновления содержимого файла, folderUrl: {0}, fileUniqueID: {1}, contentLength: {2}", folderUrl, fileUniqueID, content.LongLength); using (MemoryStream ms = new MemoryStream(content)) { using (IFile file = this.Storage.GetFile(folderUrl, fileUniqueID)) { file.Update(ms, fileName); wcfFile = WcfFileInfo.FromFile(file); } } this.Logger.WriteFormatMessage("UpdateFile:Окончание обновления содержимого файла, folderUrl: {0}, fileUniqueID: {1}, contentLength: {2}", folderUrl, fileUniqueID, content.LongLength); } catch (Exception ex) { this.Logger.WriteFormatMessage("UpdateFile:Ошибка выполенения операции, текст ошибки: {0}", ex); throw ex; } return(wcfFile); }
/// <summary> /// Загружает файл в хранилище. /// </summary> /// <param name="folderUrl">Адрес папки.</param> /// <param name="fileName">Имя файла.</param> /// <param name="content">Содержимое файла.</param> /// <returns></returns> public WcfFileInfo UploadFile(string folderUrl, string fileName, byte[] content) { WcfFileInfo wcfFile = null; try { this.Authorize(); if (string.IsNullOrEmpty(folderUrl)) { throw new ArgumentNullException("folderUrl"); } if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException("fileName"); } if (content == null) { throw new ArgumentNullException("content"); } this.Logger.WriteFormatMessage("UploadFile:Начало загрузки файла, folderUrl: {0}, fileName: {1}, contentLength: {2}", folderUrl, fileName, content.LongLength); using (MemoryStream ms = new MemoryStream(content)) { using (IFile file = this.Storage.UploadFile(folderUrl, fileName, ms)) { wcfFile = WcfFileInfo.FromFile(file); } } this.Logger.WriteFormatMessage("UploadFile:Окончание загрузки файла, folderUrl: {0}, fileName: {1}, contentLength: {2}", folderUrl, fileName, content.LongLength); } catch (Exception ex) { this.Logger.WriteFormatMessage("UploadFile:Ошибка выполенения операции, текст ошибки: {0}", ex); throw ex; } return(wcfFile); }
/// <summary> /// Возвращает файл. /// </summary> /// <param name="folderUrl">Адрес папки.</param> /// <param name="fileUniqueID">Уникальный идентификатор файла.</param> /// <param name="loadOptions">Опции загрузки.</param> /// <returns></returns> public WcfFileInfo GetFile(string folderUrl, Guid fileUniqueID, GetFileOptions loadOptions = null) { WcfFileInfo wcfFile = null; try { this.Authorize(); if (string.IsNullOrEmpty(folderUrl)) { throw new ArgumentNullException("folderUrl"); } if (fileUniqueID == Guid.Empty) { throw new ArgumentNullException("fileUniqueID"); } this.Logger.WriteFormatMessage("GetFile:Начало получения файла, folderUrl: {0}, fileUniqueID: {1}", folderUrl, fileUniqueID); //файл выбираем всегда без содержимого, чтобы не превысить размер буферной передачи, //которая известна только на уровне транспорта using (IFile file = this.Storage.GetFile(folderUrl, fileUniqueID)) { if (loadOptions != null && loadOptions.LoadContent) { this.VerifyBufferSize(file.Size); } wcfFile = WcfFileInfo.FromFile(file, loadOptions); } this.Logger.WriteFormatMessage("GetFile:Окончание получения файла, folderUrl: {0}, fileUniqueID: {1}", folderUrl, fileUniqueID); } catch (Exception ex) { this.Logger.WriteFormatMessage("GetFile:Ошибка выполенения операции, текст ошибки: {0}", ex); throw ex; } return(wcfFile); }