Ejemplo n.º 1
0
        private static DirectoryBackup BackupDirectory(IFilesStorageProviderV30 filesStorageProvider, string zipFileName,
                                                       string directory)
        {
            var directoryBackup = new DirectoryBackup();

            var files       = filesStorageProvider.ListFiles(directory);
            var filesBackup = new List <FileBackup>(files.Count());

            foreach (var file in files)
            {
                var fileDetails = filesStorageProvider.GetFileDetails(file);
                filesBackup.Add(new FileBackup
                {
                    Name         = file,
                    Size         = fileDetails.Size,
                    LastModified = fileDetails.LastModified
                });

                var tempFile = File.Create(Path.Combine(zipFileName.Trim('/').Trim('\\'), file.Trim('/').Trim('\\')));
                using (var stream = new MemoryStream())
                {
                    filesStorageProvider.RetrieveFile(file, stream, false);
                    stream.Seek(0, SeekOrigin.Begin);
                    var buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);
                    tempFile.Write(buffer, 0, buffer.Length);
                    tempFile.Close();
                }
            }
            directoryBackup.Name  = directory;
            directoryBackup.Files = filesBackup;

            var directories          = filesStorageProvider.ListDirectories(directory).ToList();
            var subdirectoriesBackup = new List <DirectoryBackup>(directories.Count);

            foreach (var d in directories)
            {
                subdirectoriesBackup.Add(BackupDirectory(filesStorageProvider, zipFileName, d));
            }
            directoryBackup.SubDirectories = subdirectoriesBackup;

            return(directoryBackup);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Backups the files storage provider.
        /// </summary>
        /// <param name="zipFileName">The zip file name where to store the backup.</param>
        /// <param name="filesStorageProvider">The files storage provider.</param>
        /// <param name="pagesStorageProviders">The pages storage providers.</param>
        /// <returns><c>true</c> if the backup file has been succesfully created.</returns>
        public static bool BackupFilesStorageProvider(string zipFileName, IFilesStorageProviderV30 filesStorageProvider, IPagesStorageProviderV30[] pagesStorageProviders)
        {
            JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();

            javascriptSerializer.MaxJsonLength = javascriptSerializer.MaxJsonLength * 10;

            string tempDir = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), Guid.NewGuid().ToString());

            Directory.CreateDirectory(tempDir);

            DirectoryBackup directoriesBackup = BackupDirectory(filesStorageProvider, tempDir, null);
            FileStream      tempFile          = File.Create(Path.Combine(tempDir, "Files.json"));

            byte[] buffer = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(directoriesBackup));
            tempFile.Write(buffer, 0, buffer.Length);
            tempFile.Close();


            // Backup Pages Attachments
            string[] pagesWithAttachment = filesStorageProvider.GetPagesWithAttachments();
            foreach (string pageWithAttachment in pagesWithAttachment)
            {
                PageInfo pageInfo = FindPageInfo(pageWithAttachment, pagesStorageProviders);
                if (pageInfo != null)
                {
                    string[] attachments = filesStorageProvider.ListPageAttachments(pageInfo);
                    List <AttachmentBackup> attachmentsBackup = new List <AttachmentBackup>(attachments.Length);
                    foreach (string attachment in attachments)
                    {
                        FileDetails attachmentDetails = filesStorageProvider.GetPageAttachmentDetails(pageInfo, attachment);
                        attachmentsBackup.Add(new AttachmentBackup()
                        {
                            Name         = attachment,
                            PageFullName = pageWithAttachment,
                            LastModified = attachmentDetails.LastModified,
                            Size         = attachmentDetails.Size
                        });
                        using (MemoryStream stream = new MemoryStream()) {
                            filesStorageProvider.RetrievePageAttachment(pageInfo, attachment, stream, false);
                            stream.Seek(0, SeekOrigin.Begin);
                            byte[] tempBuffer = new byte[stream.Length];
                            stream.Read(tempBuffer, 0, (int)stream.Length);

                            DirectoryInfo dir = Directory.CreateDirectory(Path.Combine(tempDir, Path.Combine("__attachments", pageInfo.FullName)));
                            tempFile = File.Create(Path.Combine(dir.FullName, attachment));
                            tempFile.Write(tempBuffer, 0, tempBuffer.Length);
                            tempFile.Close();
                        }
                    }
                    tempFile = File.Create(Path.Combine(tempDir, Path.Combine("__attachments", Path.Combine(pageInfo.FullName, "Attachments.json"))));
                    buffer   = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(attachmentsBackup));
                    tempFile.Write(buffer, 0, buffer.Length);
                    tempFile.Close();
                }
            }

            tempFile = File.Create(Path.Combine(tempDir, "Version.json"));
            buffer   = Encoding.Unicode.GetBytes(javascriptSerializer.Serialize(generateVersionFile("Files")));
            tempFile.Write(buffer, 0, buffer.Length);
            tempFile.Close();

            using (ZipFile zipFile = new ZipFile()) {
                zipFile.AddDirectory(tempDir, "");
                zipFile.Save(zipFileName);
            }
            Directory.Delete(tempDir, true);

            return(true);
        }
Ejemplo n.º 3
0
        private static DirectoryBackup BackupDirectory(IFilesStorageProviderV30 filesStorageProvider, string zipFileName, string directory)
        {
            DirectoryBackup directoryBackup = new DirectoryBackup();

            string[] files = filesStorageProvider.ListFiles(directory);
            List<FileBackup> filesBackup = new List<FileBackup>(files.Length);
            foreach(string file in files) {
                FileDetails fileDetails = filesStorageProvider.GetFileDetails(file);
                filesBackup.Add(new FileBackup() {
                    Name = file,
                    Size = fileDetails.Size,
                    LastModified = fileDetails.LastModified
                });

                FileStream tempFile = File.Create(Path.Combine(zipFileName.Trim('/').Trim('\\'), file.Trim('/').Trim('\\')));
                using(MemoryStream stream = new MemoryStream()) {
                    filesStorageProvider.RetrieveFile(file, stream, false);
                    stream.Seek(0, SeekOrigin.Begin);
                    byte[] buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);
                    tempFile.Write(buffer, 0, buffer.Length);
                    tempFile.Close();
                }
            }
            directoryBackup.Name = directory;
            directoryBackup.Files = filesBackup;

            string[] directories = filesStorageProvider.ListDirectories(directory);
            List<DirectoryBackup> subdirectoriesBackup = new List<DirectoryBackup>(directories.Length);
            foreach(string d in directories) {
                subdirectoriesBackup.Add(BackupDirectory(filesStorageProvider, zipFileName, d));
            }
            directoryBackup.SubDirectories = subdirectoriesBackup;

            return directoryBackup;
        }