A structure representing a container (or drive) in the NStore repository.
Esempio n. 1
0
        public void ExecuteSaveExample()
        {
            // Get some data
            var data = File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "example-files/mountain-fog.jpg");

            // Create definitions
            var file = new FileDefinition {Name = "output.jpg", Data = data};
            var container = new ContainerDefinition {Path = @"C:/temp/nstore-test/photos"};

            // File and container options
            var fileCreateOptions = Defaults<FileCreateOptions>.GetDefault();
            fileCreateOptions.OverwriteExisting = true;
            var containerCreateOptions = Defaults<ContainerCreateOptions>.GetDefault();
            containerCreateOptions.MakeFilesPublic = true;

            // Save file
            var status = _fileSystemRepository.Save(file, container, fileCreateOptions);
            Console.WriteLine("Attempting to save file \"{0}\" in {1}", file.Name, container.Path);
            Console.WriteLine(status.Message);

            // Let's move it too by specifying a new container (a clone of our container, actually) and a new name for the file
            var newContainer = new ContainerDefinition(container) {Path = "C:/temp/nstore-test2/photos"};
            var newFileName = "output-new.jpg";
            status = _fileSystemRepository.Move(file, container, newContainer, newFileName);

            Console.WriteLine("Attempting to move file \"{0}\" to {1} with new name \"{2}\"", file.Name,
                newContainer.Path, newFileName);
            Console.WriteLine(status.Message);
        }
        public override FileActionStatus Save(FileDefinition fileDefinition, ContainerDefinition containerDefinition,
            FileCreateOptions fileCreateOptions,
            ContainerCreateOptions containerCreateOptions)
        {
            var status = new FileActionStatus();

            // If we aren't allowing auto creation of directories, fail
            if (!Directory.Exists(containerDefinition.Path) && !_createPathsAutomatically)
            {
                status.Status = Status.NoAction;
                status.Message = Constants.Messages.NoAction +
                                 " Path does not exist, and config to create paths automatically is false.";
                return status;
            }

            // Create directory if it doesn't exist
            Directory.CreateDirectory(containerDefinition.Path);

            var path = GetFullFilePath(fileDefinition, containerDefinition);

            // Overwrite based on provided option
            if (!fileCreateOptions.OverwriteExisting && File.Exists(path))
            {
                status.Status = Status.NoAction;
                status.Message = Constants.Messages.NoAction +
                                 " File already exists; must explicitly set option to overwrite.";
                return status;
            }

            SaveFile(path, fileDefinition.Data).Wait();

            status.Status = Status.Success;
            status.Message = Constants.Messages.Success;
            return status;
        }
Esempio n. 3
0
        public void ExecuteTryReadExample()
        {
            // Create definitions
            var file = new FileDefinition {Name = "output.jpg"};
            var container = new ContainerDefinition {Path = @"C:/temp/nstore-test/photos"};

            // Save file
            byte[] output;
            var status = _fileSystemRepository.TryRead(file, container, out output);

            // Done!
            Console.WriteLine("Attempting to read file \"{0}\" in {1}", file.Name, container.Path);
            Console.WriteLine("Read file from repository, total {0} KB", output.LongLength/1024);
            Console.WriteLine(status.Message);
        }
Esempio n. 4
0
        public void ExecuteSaveExample()
        {
            // Get some data
            var data = File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "example-files/mountain-fog.jpg");

            // Create definitions
            var file = new FileDefinition {Name = "output.jpg", Data = data};
            var container = new ContainerDefinition {Path = @"C:/temp/nstore-test/photos"};

            // Save file
            var status = _fileSystemRepository.Save(file, container);

            // Done!
            Console.WriteLine("Attempting to save file \"{0}\" in {1}", file.Name, container.Path);
            Console.WriteLine(status.Message);
        }
 public override FileActionStatus TryRead(FileDefinition fileDefinition, ContainerDefinition containerDefinition,
     out byte[] data)
 {
     var status = new FileActionStatus();
     var path = GetFullFilePath(fileDefinition, containerDefinition);
     try
     {
         data = File.ReadAllBytes(path);
         status.Status = Status.Success;
         status.Message = Constants.Messages.Success;
     }
     catch (Exception)
     {
         data = null;
         status.Status = Status.NoAction;
         status.Message = Constants.Messages.NoAction;
     }
     return status;
 }
Esempio n. 6
0
 /// <summary>
 ///     Save a file object into the NStore repository asynchronously. If the target container doesn't exist, create it with
 ///     the provided options.
 /// </summary>
 /// <param name="fileDefinition"></param>
 /// <param name="containerDefinition"></param>
 /// <param name="fileCreateOptions"></param>
 /// <param name="containerCreateOptions"></param>
 /// <returns></returns>
 public abstract Task<FileActionStatus> SaveAsync(FileDefinition fileDefinition,
     ContainerDefinition containerDefinition,
     FileCreateOptions fileCreateOptions,
     ContainerCreateOptions containerCreateOptions);
 private string GetFullFilePath(FileDefinition fileDefinition, ContainerDefinition containerDefinition)
 {
     return Path.Combine(containerDefinition.Path, fileDefinition.Name);
 }
 public override ContainerActionStatus CreateContainer(ContainerDefinition containerDefinition,
     ContainerCreateOptions options)
 {
     throw new NotImplementedException("Do not use CreateContainer for file system storage.");
 }
        public override FileActionStatus Move(FileDefinition fileDefinition, ContainerDefinition containerDefinition,
            ContainerDefinition newContainerDefinition, string newFileName = null)
        {
            var status = new FileActionStatus();
            var newFileDefinition = new FileDefinition {Name = newFileName ?? fileDefinition.Name};
            // Don't clone fileDefinition since we don't need the data here, just the name

            var oldPath = GetFullFilePath(fileDefinition, containerDefinition);
            var newPath = GetFullFilePath(newFileDefinition, newContainerDefinition);

            // Fail early if file doesn't exist
            if (!File.Exists(oldPath))
            {
                status.Status = Status.NoAction;
                status.Message = Constants.Messages.FileDoesNotExist;
                return status;
            }

            try
            {
                // If we aren't allowing auto creation of directories, fail
                if (!Directory.Exists(newContainerDefinition.Path) && !_createPathsAutomatically)
                {
                    status.Status = Status.NoAction;
                    status.Message = Constants.Messages.NoAction +
                                     " Destination path does not exist, and config to create paths automatically is false.";
                    return status;
                }
                Directory.CreateDirectory(newContainerDefinition.Path);
                File.Move(oldPath, newPath);
                if (_removeEmptyPathsOnMove) RemoveEmptyDirectories(containerDefinition.Path);
            }
            catch (Exception)
            {
                status.Status = Status.NoAction;
                status.Message = Constants.Messages.NoAction;
            }

            status.Status = Status.Success;
            status.Message = Constants.Messages.Success;
            return status;
        }
        public override FileActionStatus Delete(FileDefinition fileDefinition, ContainerDefinition containerDefinition)
        {
            var status = new FileActionStatus();
            var path = GetFullFilePath(fileDefinition, containerDefinition);

            // Fail early if file doesn't exist
            if (!File.Exists(path))
            {
                status.Status = Status.NoAction;
                status.Message = Constants.Messages.FileDoesNotExist;
                return status;
            }

            try
            {
                File.Delete(path);
                if (_removeEmptyPathsOnDelete) RemoveEmptyDirectories(containerDefinition.Path);
            }
            catch (Exception)
            {
                status.Status = Status.NoAction;
                status.Message = Constants.Messages.NoAction;
            }

            status.Status = Status.Success;
            status.Message = Constants.Messages.Success;
            return status;
        }
Esempio n. 11
0
 /// <summary>
 ///     Save a file object into the NStore repository with default options. If the target container doesn't exist, create
 ///     it with default options.
 /// </summary>
 /// <param name="fileDefinition"></param>
 /// <param name="containerDefinition"></param>
 /// <param name="fileCreateOptions"></param>
 /// <returns></returns>
 public FileActionStatus Save(FileDefinition fileDefinition, ContainerDefinition containerDefinition,
     FileCreateOptions fileCreateOptions)
 {
     return Save(fileDefinition, containerDefinition, fileCreateOptions,
         Defaults<ContainerCreateOptions>.GetDefault());
 }
Esempio n. 12
0
 /// <summary>
 ///     Save a file object into the NStore repository asynchronously. If the target container doesn't exist, create it with
 ///     default options.
 /// </summary>
 /// <param name="fileDefinition"></param>
 /// <param name="containerDefinition"></param>
 /// <returns></returns>
 public Task<FileActionStatus> SaveAsync(FileDefinition fileDefinition, ContainerDefinition containerDefinition)
 {
     return SaveAsync(fileDefinition, containerDefinition, Defaults<FileCreateOptions>.GetDefault(),
         Defaults<ContainerCreateOptions>.GetDefault());
 }
Esempio n. 13
0
 /// <summary>
 ///     Save a file object into the NStore repository. If the target container doesn't exist, create it with the provided
 ///     options.
 /// </summary>
 /// <param name="fileDefinition"></param>
 /// <param name="containerDefinition"></param>
 /// <param name="fileCreateOptions"></param>
 /// <param name="containerCreateOptions"></param>
 /// <returns></returns>
 public abstract FileActionStatus Save(FileDefinition fileDefinition, ContainerDefinition containerDefinition,
     FileCreateOptions fileCreateOptions,
     ContainerCreateOptions containerCreateOptions);
Esempio n. 14
0
 /// <summary>
 ///     Move a file object in the NStore repository. Also used for rename.
 /// </summary>
 /// <param name="fileDefinition"></param>
 /// <param name="containerDefinition"></param>
 /// <param name="newContainerDefinition"></param>
 /// <param name="newFileName"></param>
 /// <returns></returns>
 public abstract FileActionStatus Move(FileDefinition fileDefinition, ContainerDefinition containerDefinition,
     ContainerDefinition newContainerDefinition, string newFileName = null);
Esempio n. 15
0
 /// <summary>
 ///     Delete a file object from the NStore repository.
 /// </summary>
 /// <param name="fileDefinition"></param>
 /// <param name="containerDefinition"></param>
 /// <returns></returns>
 public abstract FileActionStatus Delete(FileDefinition fileDefinition, ContainerDefinition containerDefinition);
Esempio n. 16
0
 /// <summary>
 ///     Create a container in the NStore repository.
 /// </summary>
 /// <param name="containerDefinition"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public abstract ContainerActionStatus CreateContainer(ContainerDefinition containerDefinition,
     ContainerCreateOptions options);
Esempio n. 17
0
 /// <summary>
 ///     Read a file from the NStore repository.
 /// </summary>
 /// <param name="fileDefinition"></param>
 /// <param name="containerDefinition"></param>
 /// <param name="data"></param>
 /// <returns></returns>
 public abstract FileActionStatus TryRead(FileDefinition fileDefinition, ContainerDefinition containerDefinition,
     out byte[] data);