public ImageFileData GetImageByName(string request_file_name) { Log("GetImageByName"); try { if (string.IsNullOrEmpty(request_file_name)) throw new FaultException<InvalidFileName>(new InvalidFileName { InvalidName = request_file_name }, new FaultReason (string.Empty)); IEnumerable<FileInfo> allImageFilesList = GetAllImageFiles(); FileInfo requestedImageFile = null; requestedImageFile = allImageFilesList.SingleOrDefault(f => f.Name == request_file_name); if (requestedImageFile == null) throw new FaultException<FileNotFound>(new FileNotFound { FileName = request_file_name }, new FaultReason(string.Empty)); ImageFileData imageFileData = new ImageFileData() { FileName = requestedImageFile.Name, LastDateModified = requestedImageFile.LastWriteTime }; byte[] imageBytes = File.ReadAllBytes(requestedImageFile.FullName); imageFileData.ImageData = imageBytes; return imageFileData; } catch (FaultException e) { throw e; } catch (Exception e) { HostStorageException fault = new HostStorageException(); fault.Description = "Prodlems with images storage on host"; throw new FaultException<HostStorageException>(fault, new FaultReason(string.Empty)); } }
public IEnumerable<ImageFileData> GetAllImagesList(bool withFilesData) { Log("GetAllImagesList"); List<ImageFileData> images_collection = new List<ImageFileData>(); try { IEnumerable<FileInfo> allImageFiles = GetAllImageFiles(); if (allImageFiles == null) return null; foreach (FileInfo fileInfo in allImageFiles) { ImageFileData imageFileData = new ImageFileData() { FileName = fileInfo.Name, LastDateModified = fileInfo.LastWriteTime }; byte[] imageBytes = null; if (withFilesData) imageBytes = File.ReadAllBytes(fileInfo.FullName); imageFileData.ImageData = imageBytes; images_collection.Add(imageFileData); } } catch (Exception) { HostStorageException fault = new HostStorageException(); fault.Description = "Prodlems with images storage on host"; throw new FaultException<HostStorageException>(fault, new FaultReason (string.Empty)); } return images_collection; }
public void UploadImage(ImageFileData uploading_image) { Log("UploadImage"); try { if (string.IsNullOrEmpty(uploading_image.FileName)) throw new ArgumentException("Invalid upldoading file name", uploading_image.FileName); if (uploading_image.ImageData == null || uploading_image.ImageData.Length == 0) throw new ArgumentException("Uploaded file-data is empty!"); string newImageFileName = uploading_image.FileName; string uploadFolder = ConfigurationManager.AppSettings["UploadFolder"]; string newImageFilePath = Path.Combine(uploadFolder, newImageFileName); if (File.Exists(newImageFilePath)) throw new FaultException<FileAlreadyExists>(new FileAlreadyExists { FileName = newImageFileName }, new FaultReason(string.Empty)); using (Stream targetStream = new FileStream(newImageFilePath, FileMode.OpenOrCreate, FileAccess.Write)) { targetStream.Write(uploading_image.ImageData, 0, uploading_image.ImageData.Length); } } catch (FaultException e) { throw e; } catch (Exception) { HostStorageException fault = new HostStorageException(); fault.Description = "Prodlems with images storage on host"; throw new FaultException<HostStorageException>(fault, new FaultReason(fault.Description)); } }