public Entities.Storage.Directory RetrieveUploadDirectory()
        {
            return(this.cachingService.GetOrSet("UploadDirectory", () =>
            {
                using (var ctx = dbContextFactory.Create())
                {
                    var directoryDbSet = ctx.Set <Entities.Storage.Directory>();
                    var locationDbSet = ctx.Set <Entities.Storage.Location>();

                    var directory = directoryDbSet.Include(x => x.Location).FirstOrDefault(x => x.Id == UPLOAD_STRING);

                    if (directory == null)
                    {
                        var location = locationDbSet.FirstOrDefault(x => x.Id == UPLOAD_STRING);

                        if (location == null)
                        {
                            var settings = settingsService.Get();

                            location = new Entities.Storage.Location
                            {
                                Id = UPLOAD_STRING,
                                Volume = UPLOAD_STRING,
                                RPath = settings.DocumentsPath
                            };

                            locationDbSet.Add(location);
                        }

                        directory = new Entities.Storage.Directory
                        {
                            Id = UPLOAD_STRING,
                            LocationId = UPLOAD_STRING,
                            Name = "ALL",
                            Location = location
                        };

                        directoryDbSet.Add(directory);

                        ctx.SaveChanges();
                    }

                    return directory;
                }
            }));
        }
        public FileResult GenerateUniqueFilePath(Entities.Storage.Directory directory, Entities.Storage.Location location, string fileExtension)
        {
            var fullDirPath = this.GetDirectoryPath(directory, location);

            string filePath = string.Empty;

            do
            {
                filePath = Path.Combine(fullDirPath, Guid.NewGuid().ToString() + fileExtension);
            }while (File.Exists(filePath));

            return(new FileResult
            {
                AbsolutePath = filePath,
                OdissDirectory = directory
            });
        }
        public FileResult SaveFile(byte[] bytes, string filename, Entities.Storage.Directory directory)
        {
            var filePath = Path.Combine(GetDirectoryPath(directory, directory.Location), filename);

            var saveFileResult = new FileResult
            {
                AbsolutePath   = filePath,
                OdissDirectory = directory
            };

            if (!Directory.Exists(saveFileResult.DirectoryName))
            {
                Directory.CreateDirectory(saveFileResult.DirectoryName);
            }

            using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                fileStream.Write(bytes, 0, bytes.Length);

                return(saveFileResult);
            }
        }
        private string GetDirectoryPath(Entities.Storage.Directory directory, Entities.Storage.Location location)
        {
            var settings = settingsService.Get();

            return(Path.Combine(location.RPath ?? settings.DocumentsPath, location.Volume, directory.Name));
        }