Example #1
0
        //-----------------------------------------------------------------------------------
        public ActionResult File(int id)
        {
            var f = _docRepo.Get(id);

            if (f != null)
            {
                var document = f.BinaryBody;
                var cd       = new System.Net.Mime.ContentDisposition {
                    FileName = f.DocName,
                    Inline   = true,
                };

                Response.AppendHeader("Content-Disposition", cd.ToString());

                var      mtr          = new MimeTypeResolver();
                MimeType oExtMimeType = mtr.Get(f.DocName);

                FileResult fs = new FileContentResult(f.BinaryBody, oExtMimeType.PrimaryMimeType);
                Log.Debug("fs {1} mime type {0}", oExtMimeType, f.DocName);

                if (fs.ContentType.Contains("image") ||
                    fs.ContentType.Contains("pdf") ||
                    fs.ContentType.Contains("html") ||
                    fs.ContentType.Contains("text"))
                {
                    return(fs);
                }

                var pdfDocument = AgreementRenderer.ConvertToPdf(document);

                if (pdfDocument != null)
                {
                    return(File(pdfDocument, "application/pdf"));
                }

                return(fs);
            }
            return(null);
        }
Example #2
0
        }         // ToString

        public string DetectFileMimeType(byte[] oFilePrefix, string sFileName, int?nFilePrefixLength = 256, ASafeLog oLog = null)
        {
            oLog = oLog.Safe();

            var mtr = new MimeTypeResolver();

            MimeType oExtMimeType = mtr.Get(sFileName);

            if (oExtMimeType != null)
            {
                oLog.Debug("MIME type by file extension is {0}.", oExtMimeType.PrimaryMimeType);

                if (!this.Contains(oExtMimeType.PrimaryMimeType))
                {
                    oLog.Debug("MIME type by file extension {0} does not conform to this limitation {1}.", oExtMimeType.PrimaryMimeType, this);
                    return(null);
                }                 // if
            }
            else
            {
                oLog.Debug("MIME type cannot be detected by file name {0}.", sFileName);
            }

            if (oFilePrefix != null)
            {
                string sFileMimeType = mtr.GetFromFile(oFilePrefix, nFilePrefixLength);

                oLog.Debug("MIME type by content is {0}", sFileMimeType);

                if (this.Contains(sFileMimeType))
                {
                    oLog.Debug("MIME type by content {0} conforms to this limitation {1}.", sFileMimeType, this);
                    return(sFileMimeType);
                }                 // if

                oLog.Debug("MIME type by content {0} does not conform to this limitation {1}.", sFileMimeType, this);

                MimeType oFileMimeType = mtr.Find(sFileMimeType);

                if (oFileMimeType == null)
                {
                    oFileMimeType = MimeType.Binary;
                    oLog.Debug("Could not find MIME type by its name '{0}', using default MIME type '{1}'.", sFileMimeType, oFileMimeType.PrimaryMimeType);
                }                 // if

                if ((oExtMimeType == null) && oFileMimeType.IsCommon)
                {
                    oLog.Debug(
                        "MIME type by file ('{0}') is a common one and MIME type by extension cannot be detected - file should be dropped.",
                        oFileMimeType.PrimaryMimeType
                        );
                    return(null);
                }                 // if

                if (oFileMimeType * oExtMimeType)
                {
                    oLog.Debug(
                        "MIME type by file ('{0}') differs from MIME type by extension ('{1}') but they are compatible so using the latter.",
                        oFileMimeType.PrimaryMimeType,
                        oExtMimeType == null ? "-- null --" : oExtMimeType.PrimaryMimeType
                        );

                    return(oExtMimeType == null ? null : oExtMimeType.PrimaryMimeType);
                }                 // if

                oLog.Debug(
                    "MIME type by file ('{0}') differs from MIME type by extension ('{1}') and they are not compatible.",
                    oFileMimeType.PrimaryMimeType,
                    oExtMimeType == null ? string.Empty : oExtMimeType.PrimaryMimeType
                    );

                return(null);
            }             // if

            if (oExtMimeType != null)
            {
                oLog.Debug("MIME type by file extension {0} conforms to this limitation {1}.", oExtMimeType.PrimaryMimeType, this);
                return(oExtMimeType.PrimaryMimeType);
            }             // if

            oLog.Debug("No MIME type detected, returning null.");
            return(null);
        }         // DetectFileMimeType