Пример #1
0
		public void constructor_should_fill_properties_and_create_empty_files_and_childfolders()
		{
			// Arrange
			string name = "random.jpg";
			string extension = "jpg";
			long size = 1241241;
			DateTime createDate = DateTime.Today;
			string folder = "/Home/MyDirectory";

			// Act
			FileViewModel model = new FileViewModel(name, extension, size, createDate, folder);		

			// Assert
			Assert.That(model.Name, Is.EqualTo(name));
			Assert.That(model.Extension, Is.EqualTo(extension));
			Assert.That(model.Size, Is.EqualTo(size));
			Assert.That(model.CreateDate, Is.EqualTo(createDate.ToShortDateString()));
			Assert.That(model.Folder, Is.EqualTo(folder));
		}
Пример #2
0
        public DirectoryViewModel FolderInfo(string dir)
        {
            try
            {
                string currentFolderName = dir;
                if (!string.IsNullOrEmpty(currentFolderName) && currentFolderName != "/")
                    currentFolderName = Path.GetFileName(dir);

                DirectoryViewModel directoryModel = new DirectoryViewModel(currentFolderName, dir);
                CloudBlobContainer container = GetCloudBlobContainer();
                CloudBlobDirectory azureDirectory = container.GetDirectoryReference(dir);
                List<CloudBlobDirectory> directories = azureDirectory.ListBlobs()
                                                                    .Select(b => b as CloudBlobDirectory)
                                                                    .Where(b => b != null)
                                                                    .ToList();

                List<CloudBlockBlob> files = azureDirectory.ListBlobs()
                                                          .OfType<CloudBlockBlob>()
                                                          .Where(b => !FilesToExclude.Contains(Path.GetFileName(b.Name)))
                                                          .ToList();

                foreach (CloudBlobDirectory directory in directories)
                {
                    string dirName = directory.Prefix.TrimEnd('/');
                    dirName = dirName.Replace(directory.Parent.Prefix, String.Empty);
                    DirectoryViewModel childModel = new DirectoryViewModel(dirName, directory.Prefix.TrimEnd('/'));
                    directoryModel.ChildFolders.Add(childModel);
                }

                foreach (CloudBlockBlob file in files)
                {
                    file.FetchAttributes();
                    string filename = file.Name;
                    FileViewModel fileModel = new FileViewModel(Path.GetFileName(file.Name), Path.GetExtension(file.Name).Replace(".", ""), file.Properties.Length, ((DateTimeOffset)file.Properties.LastModified).DateTime, dir);
                    directoryModel.Files.Add(fileModel);
                }

                return directoryModel;
            }
            catch (StorageException e)
            {
                throw new FileException(e.Message, e);
            }
        }
Пример #3
0
        public ActionResult FolderInfo(string dir)
        {
            if (!Directory.Exists(ApplicationSettings.AttachmentsDirectoryPath))
                return Json(new { status = "error", message = "The attachments directory does not exist - please create it." });

            string folder = dir;
            folder = Server.UrlDecode(folder);

            string physicalPath = _attachmentPathUtil.ConvertUrlPathToPhysicalPath(folder);

            if (!_attachmentPathUtil.IsAttachmentPathValid(physicalPath))
            {
                throw new SecurityException(null, "Attachment path was invalid when getting the folder {0}", dir);
            }

            try
            {
                string currentFolderName = dir;
                if (!string.IsNullOrEmpty(currentFolderName) && currentFolderName != "/")
                    currentFolderName = Path.GetFileName(dir);

                DirectoryViewModel directoryModel = new DirectoryViewModel(currentFolderName, dir);
                if (Directory.Exists(physicalPath))
                {
                    foreach (string directory in Directory.GetDirectories(physicalPath))
                    {
                        DirectoryInfo info = new DirectoryInfo(directory);
                        string fullPath = info.FullName;
                        fullPath = fullPath.Replace(ApplicationSettings.AttachmentsDirectoryPath, "");
                        fullPath = fullPath.Replace(Path.DirectorySeparatorChar.ToString(), "/");
                        fullPath = "/" + fullPath; // removed in the 1st replace

                        DirectoryViewModel childModel = new DirectoryViewModel(info.Name, fullPath);
                        directoryModel.ChildFolders.Add(childModel);
                    }

                    foreach (string file in Directory.GetFiles(physicalPath))
                    {

                        FileInfo info = new FileInfo(file);
                        string filename = info.Name;

                        if (!_filesToExclude.Contains(info.Name))
                        {
                            string urlPath = Path.Combine(dir, filename);
                            FileViewModel fileModel = new FileViewModel(info.Name, info.Extension.Replace(".", ""), info.Length, info.CreationTime, dir);
                            directoryModel.Files.Add(fileModel);
                        }
                    }
                }

                return Json(directoryModel);
            }
            catch (IOException e)
            {
                return Json(new { status = "error", message = "An unhandled error occurred: "+e.Message });
            }
        }