public void Save(MediaFile mediaFile, MediaStorageItem item)
        {
            Guard.NotNull(mediaFile, nameof(mediaFile));

            // TODO: (?) if the new file extension differs from the old one then the old file never gets deleted

            var filePath = GetPath(mediaFile);

            if (item != null)
            {
                // Create folder if it does not exist yet
                var dir = Path.GetDirectoryName(filePath);
                if (!_fileSystem.FolderExists(dir))
                {
                    _fileSystem.CreateFolder(dir);
                }

                using (item)
                {
                    using var outStream = _fileSystem.GetFile(filePath).OpenWrite();
                    item.SaveTo(outStream, mediaFile);
                }
            }
            else if (_fileSystem.FileExists(filePath))
            {
                // Remove media storage if any
                _fileSystem.DeleteFile(filePath);
            }
        }
        private void DownloadDir(string path)
        {
            path = GetRelativePath(path);
            if (!_fileSystem.FolderExists(path))
            {
                throw new Exception(LangRes("E_CreateArchive"));
            }

            var folder = _fileSystem.GetFolder(path);

            // copy files from file storage to temp folder
            var tempDir = FileSystemHelper.TempDirTenant("roxy " + folder.Name);

            FileSystemHelper.ClearDirectory(tempDir, false);
            var files = GetFiles(path, null);

            foreach (var file in files)
            {
                var bytes = _fileSystem.ReadAllBytes(file.Path);
                if (bytes != null && bytes.Length > 0)
                {
                    System.IO.File.WriteAllBytes(Path.Combine(tempDir, file.Name), bytes);
                }
            }

            // create zip from temp folder
            var tempZip = Path.Combine(FileSystemHelper.TempDirTenant(), folder.Name + ".zip");

            FileSystemHelper.DeleteFile(tempZip);

            ZipFile.CreateFromDirectory(tempDir, tempZip, CompressionLevel.Fastest, false);

            Response.Clear();
            Response.Headers.Add("Content-Disposition", "attachment; filename=\"" + folder.Name + ".zip\"");
            Response.ContentType = "application/zip";
            Response.TransmitFile(tempZip);
            Response.Flush();

            FileSystemHelper.DeleteFile(tempZip);
            FileSystemHelper.ClearDirectory(tempDir, true);

            Response.End();
        }
Пример #3
0
        public void CacheStatistics(out long fileCount, out long totalSize)
        {
            fileCount = 0;
            totalSize = 0;

            if (!_fileSystem.FolderExists(_thumbsRootDir))
            {
                return;
            }

            fileCount = _fileSystem.SearchFiles(_thumbsRootDir, "*.*").Count();
            totalSize = _fileSystem.ListFolders(_thumbsRootDir).Sum(x => x.Size);
        }