Exemple #1
0
        /*
         * Convert the contents of the supplied directory into an RSMF file that can be ingested into Relativity via
         * Relativity processing.  The inputDirectory must contain a file named rsmf_manifest.json.  Any other files
         * in the directory will also be included in the RSMF regardless of if they are referenced by the
         * manifest files or not.  The outputFile will be where the RSMF file is written to.
         */
        public void GenerateRSMF(DirectoryInfo inputDirectory, FileInfo outputFile)
        {
            FileInfo manifest = null;

            if (!inputDirectory.Exists)
            {
                throw new Exception($"The input directory {inputDirectory.FullName} doesn't exist.");
            }

            if (!File.Exists(Path.Combine(inputDirectory.FullName, "rsmf_manifest.json")))
            {
                throw new Exception($"The file rsmf_manifest.json does not exist in {inputDirectory.FullName}.");
            }
            manifest = inputDirectory.GetFiles("rsmf_manifest.json", SearchOption.TopDirectoryOnly)?[0];

            /*
             * Create a stream to a zip that contains the rsmf_manifest.json file and any other files that exist in its
             * directory.
             */
            using (Stream rsmf_zip = CreateRSMFZip(inputDirectory))
            {
                /*
                 * Validating the zip stream validates that the JSON in the rsmf_manifest.json file is compliant with the
                 * RSMF specification.  It also verifies that the attachment references in rsmf_manifest.json have equivalent
                 * files in the Zip.
                 */
                if (ValidateZip)
                {
                    IValidator          zipValidator = RSMFValidatorFactory.GetRSMFZipValidator();
                    RSMFValidatorResult results      = zipValidator.PerformTests(rsmf_zip);

                    /*
                     * This code only reports errors, not warnings.  Warnings from the validator are
                     * contained in the results.Warnings list.
                     */
                    if (results.Result == ResultTypes.Failed)
                    {
                        StringBuilder errorString = new StringBuilder();
                        foreach (IValidatorError error in results.Errors)
                        {
                            errorString.AppendLine();
                            errorString.AppendLine(error.ErrorMessage);
                        }
                        throw new Exception("Validation of the generated ZIP failed: " + errorString.ToString());
                    }
                }

                /* Leverage MimeKit to create the representation of the RSMF file.*/
                MimeMessage rsmf = CreateRSMF(manifest);
                /* MimePart defaults to application/octet-stream, which is exactly what is necessary.*/
                MimePart attachment = new MimePart();
                attachment.Content            = new MimeContent(rsmf_zip);
                attachment.ContentDisposition = new ContentDisposition(ContentDisposition.Attachment);
                /* The RSMF specification requires that the name of the attachment be rsmf.zip */
                attachment.FileName = "rsmf.zip";

                /*
                 * This is a trick to make it easier to add the zip attachment to the RSMF.  It is known that the Body property
                 * of RSMF returned by CreateRSMF is a Multipart object, so this cast is safe.
                 */
                ((Multipart)rsmf.Body).Add(attachment);

                /*
                 * Prepare the final EML before writing it out.  This accomplishes various things like making sure the body
                 * text is encoded correctly.  For RSMF files, the encoding constraint is 7-bit.
                 */
                rsmf.Prepare(EncodingConstraint.SevenBit);
                /* Finally, write the object to the provided file. */
                rsmf.WriteTo(outputFile.FullName);
            }
        }
Exemple #2
0
        private static void ValidateMyFile(string filepath)
        {
            //RSMF Validation library validates 3 types of files: .rsmf, rsmf_manifest.json and rsmf.zip against the RSMF specification
            //Select the validation method based on the file type
            string fileExt = Path.GetExtension(filepath);

            try
            {
                IValidator myValidator; //An IValidator object is used to validate an RSMF component. IValidators are created by the RSMFValidatorFactory
                //ResultTypes enumerate the possible values for the Result property
                //ResultTypes are Passed, Failed or PassedWithWarnings
                RSMFValidatorResult result; //The overall results of running a validation

                //Use the PerformTests method (Stream or String) to check if the file passes validation or create a log if it passed with warnings or failed
                //PerformTests returns an RSMFValidatorResult object
                //Use the Dispose method release unmanaged resources used by your application
                switch (fileExt)
                {
                case ".rsmf":
                    myValidator = RSMFValidatorFactory.GetRSMFValidator();     //Create a validator that validates an RSMF file
                    result      = myValidator.PerformTests(filepath);
                    Console.WriteLine($"Result for {filepath}: {result.Result}");
                    if (result.Result == ResultTypes.Failed || result.Result == ResultTypes.PassedWithWarnings)
                    {
                        CreateLog(filepath, result);
                    }
                    myValidator.Dispose();
                    break;

                case ".json":
                    myValidator = RSMFValidatorFactory.GetRSMFJsonValidator();     //Create a validator that validates the contents of an rsmf_manifest.json file
                    result      = myValidator.PerformTests(filepath);
                    Console.WriteLine($"Result for {filepath}: {result.Result}");

                    if (result.Result == ResultTypes.Failed || result.Result == ResultTypes.PassedWithWarnings)
                    {
                        CreateLog(filepath, result);
                    }
                    myValidator.Dispose();
                    break;

                case ".zip":
                    myValidator = RSMFValidatorFactory.GetRSMFZipValidator();     //Create a validator that validates an RSMF ZIP file
                    result      = myValidator.PerformTests(filepath);
                    Console.WriteLine($"Result for {filepath}: {result.Result}");
                    if (result.Result == ResultTypes.Failed || result.Result == ResultTypes.PassedWithWarnings)
                    {
                        CreateLog(filepath, result);
                    }
                    myValidator.Dispose();
                    break;

                default:
                    Console.WriteLine($"Not a valid extension for RSMF Validation: {filepath}");
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"Unable to complete validation test:{Environment.NewLine}{ex.Message}");
            }
        }