Beispiel #1
0
        private static bool ParseAttachmentList(string attachmentList, AttachmentCollection result, IList <IDisposable> toDispose)
        {
            if (attachmentList.IsNullOrEmpty())
            {
                return(true);
            }

            foreach (var atmStr in attachmentList.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries))
            {
                string line = atmStr.Trim();
                if (line.IsNullOrEmpty())
                {
                    continue;
                }

                string attachmentName;
                string filePath;
                string mimeType;

                if (line.Contains(","))
                {
                    int comaIndex = line.IndexOf(',');
                    mimeType = line.Substring(comaIndex + 1);
                    line     = line.Substring(0, comaIndex).Trim();
                }
                else
                {
                    mimeType = null;
                }


                if (line.Contains("="))
                {
                    int equalSignIndex = line.IndexOf('=');
                    attachmentName = line.Substring(0, equalSignIndex).Trim();
                    filePath       = line.Substring(equalSignIndex + 1).Trim();
                }
                else
                {
                    attachmentName = null;
                    filePath       = line;
                }

                // Checking whether the file is from Composite media database
                if (filePath.StartsWith(CompositeMediaAttachmentPrefix))
                {
                    string     compositePath = filePath.Substring(CompositeMediaAttachmentPrefix.Length);
                    string     storeId       = compositePath.Substring(0, compositePath.IndexOf(':'));
                    IMediaFile mediaFile;
                    try
                    {
                        mediaFile = DataFacade.GetData <IMediaFile>(f => f.StoreId == storeId && f.CompositePath == compositePath).FirstOrDefault();
                    }
                    catch (Exception e)
                    {
                        LoggingService.LogWarning(LogTitle, "Media file '{0}' cannot be found.".FormatWith(compositePath));
                        LoggingService.LogError(LogTitle, e);
                        return(false);
                    }

                    Stream readStream = mediaFile.GetReadStream();

                    result.Add(new Attachment(readStream, attachmentName ?? mediaFile.Title, mimeType ?? mediaFile.MimeType));

                    continue;
                }

                // File is a file on disk
                if (!filePath.Contains(@":\") && HttpContext.Current != null)
                {
                    filePath = HttpContext.Current.Server.MapPath(filePath);
                }

                if (!C1File.Exists(filePath))
                {
                    LoggingService.LogWarning(LogTitle,
                                              "Cannot create an attachment. File '{0}' does not exists".
                                              FormatWith(filePath));
                    return(false);
                }


                Attachment attachment;
                if (mimeType.IsNullOrEmpty())
                {
                    if (attachmentName.IsNullOrEmpty())
                    {
                        attachment = new Attachment(filePath);
                    }
                    else
                    {
                        attachment = new Attachment(C1File.OpenRead(filePath), attachmentName);
                    }
                }
                else
                {
                    if (attachmentName.IsNullOrEmpty())
                    {
                        attachment = new Attachment(filePath, mimeType);
                    }
                    else
                    {
                        attachment = new Attachment(C1File.OpenRead(filePath), attachmentName, mimeType);
                    }
                }

                result.Add(attachment);
            }

            return(true);
        }
Beispiel #2
0
        private static void DoAutoInstallPackages()
        {
            if (IsReinitializingTheSystem)
            {
                return;
            }

            try
            {
                // This is not so good, unittests run and normal runs should have same semantic behavior.
                // But if this is not here, some unittests will start failing. /MRJ
                if (RuntimeInformation.IsUnittest)
                {
                    return;
                }

                var zipFiles = new List <AutoInstallPackageInfo>();

                string directory = PathUtil.Resolve(GlobalSettingsFacade.AutoPackageInstallDirectory);
                if (C1Directory.Exists(directory))
                {
                    Log.LogVerbose(LogTitle, string.Format("Installing packages from: {0}", directory));
                    zipFiles.AddRange(C1Directory.GetFiles(directory, "*.zip")
                                      .Select(f => new AutoInstallPackageInfo {
                        FilePath = f, ToBeDeleted = true
                    }));
                }
                else
                {
                    Log.LogVerbose(LogTitle, string.Format("Auto install directory not found: {0}", directory));
                }

                if (RuntimeInformation.IsDebugBuild)
                {
                    string workflowTestDir = Path.Combine(PathUtil.Resolve(GlobalSettingsFacade.AutoPackageInstallDirectory), "WorkflowTesting");
                    if (C1Directory.Exists(workflowTestDir))
                    {
                        Log.LogVerbose(LogTitle, string.Format("Installing packages from: {0}", workflowTestDir));
                        zipFiles.AddRange(C1Directory.GetFiles(workflowTestDir, "*.zip")
                                          .OrderBy(f => f)
                                          .Select(f => new AutoInstallPackageInfo {
                            FilePath = f, ToBeDeleted = false
                        }));
                    }
                }


                foreach (var zipFile in zipFiles)
                {
                    try
                    {
                        using (Stream zipFileStream = C1File.OpenRead(zipFile.FilePath))
                        {
                            Log.LogVerbose(LogTitle, "Installing package: " + zipFile.FilePath);

                            PackageManagerInstallProcess packageManagerInstallProcess = PackageManager.Install(zipFileStream, true);

                            if (packageManagerInstallProcess.PreInstallValidationResult.Count > 0)
                            {
                                Log.LogError(LogTitleNormal, "Package installation failed! (Pre install validation error)");
                                LogErrors(packageManagerInstallProcess.PreInstallValidationResult);

                                continue;
                            }


                            List <PackageFragmentValidationResult> validationResults = packageManagerInstallProcess.Validate();
                            if (validationResults.Count > 0)
                            {
                                Log.LogError(LogTitleNormal, "Package installation failed! (Validation error)");
                                LogErrors(validationResults);

                                continue;
                            }


                            List <PackageFragmentValidationResult> installResult = packageManagerInstallProcess.Install();
                            if (installResult.Count > 0)
                            {
                                Log.LogError(LogTitleNormal, "Package installation failed! (Installation error)");
                                LogErrors(installResult);

                                continue;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.LogWarning(LogTitleNormal, ex);
                    }

                    if (zipFile.ToBeDeleted)
                    {
                        FileUtils.Delete(zipFile.FilePath);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.LogError(LogTitleNormal, ex);
            }
        }
Beispiel #3
0
    protected string CreateBackup()
    {
        var    Url         = Request.Url;
        string zipFilename = Url.Scheme + "_" + Url.Host + (Url.IsDefaultPort ? "" : (" " + Url.Port)) + "_" +
                             DateTime.Now.ToString("yyyy_MM.dd_HHmm") + ".zip";
        string zipFilePath = PathCombine(BackupDirectory, zipFilename);

        string backupDirectory = Path.GetDirectoryName(zipFilePath);

        if (!C1Directory.Exists(backupDirectory))
        {
            C1Directory.CreateDirectory(backupDirectory);
        }


        var filenames   = GetFilesRecursive(BaseDirectory);
        var directories = GetDirectoriesRecursive(BaseDirectory);

        lock (_lock)
        {
            using (var archiveFileStream = FileCreate(zipFilePath))
                using (var archive = new ZipArchive(archiveFileStream, ZipArchiveMode.Create))
                {
                    int oldScriptTimeOut = Server.ScriptTimeout;
                    Server.ScriptTimeout = ScriptTimeoutInSeconds;

                    foreach (string directory in directories)
                    {
                        if (directory.Contains(BackupDirectoryRelativePath))
                        {
                            continue;
                        }

                        string directoryEntryName = directory.Replace(BaseDirectory, "").Replace("\\", "/") + "/";

                        var dirEntry = archive.CreateEntry(directoryEntryName);
                        dirEntry.LastWriteTime = Directory.GetLastWriteTime(directory);
                    }

                    foreach (string file in filenames)
                    {
                        if (file.Contains(BackupDirectoryRelativePath) || IsTemporaryDirectory(file))
                        {
                            continue;
                        }

                        string fileEntryName = file.Replace(BaseDirectory, "").Replace("\\", "/");

                        var entry = archive.CreateEntry(fileEntryName, BackupCompressionLevel);

                        try
                        {
                            var fi = new C1FileInfo(file);
                            entry.LastWriteTime = fi.LastWriteTime;

                            using (var fileStream = C1File.OpenRead(file))
                                using (var entryStream = entry.Open())
                                {
                                    fileStream.CopyTo(entryStream);
                                }
                        }
                        catch (Exception e)
                        {
                            if (!file.Contains("App_Data\\Composite\\LogFiles"))
                            {
                                this.Validators.Add(new ErrorSummary(e.Message));
                            }
                        }
                    }

                    Server.ScriptTimeout = oldScriptTimeOut;
                }
        }

        return(zipFilename);
    }