Example #1
0
        public IActionResult GetImageQualById(string filename, string qlty)
        {
            DownloadFileResponse dfs = null;

            HttpContext.Response.Headers[HeaderNames.CacheControl] = "private, max-age=31536000";
            ActionResult resp = new EmptyResult();

            DownloadImageByIdRequest dfq = new DownloadImageByIdRequest();

            try
            {
                dfq.ImageInfo = new ImageMeta {
                    FileRefId = Convert.ToInt32(filename.SplitOnLast(CharConstants.DOT).First()), FileCategory = EbFileCategory.Images, ImageQuality = Enum.Parse <ImageQuality>(qlty)
                };

                Console.WriteLine("Image Info: " + dfq.ImageInfo.ToJson());

                this.FileClient.Timeout = new TimeSpan(0, 5, 0);

                dfs = this.FileClient.Get <DownloadFileResponse>(dfq);

                if (dfs.StreamWrapper != null)
                {
                    Console.WriteLine("Image Size: " + dfs.StreamWrapper.Memorystream.Length);

                    dfs.StreamWrapper.Memorystream.Position = 0;
                    resp = new FileStreamResult(dfs.StreamWrapper.Memorystream, GetMime(filename));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.StackTrace.ToString() + e.Message.ToString());
            }
            return(resp);
        }
        public DownloadFileResponse Get(DownloadImageByIdRequest request)
        {
            Console.WriteLine("In DownloadImageByIdRequest");

            byte[] fb = new byte[0];

            string sFilePath = string.Format("../StaticFiles/{0}/{1}/{2}", request.SolnId, request.ImageInfo.ImageQuality, request.ImageInfo.FileRefId);

            MemoryStream ms = null;

            DownloadFileResponse dfs = new DownloadFileResponse();

            Console.WriteLine("Download Image Info: " + request.ImageInfo.ToJson());

            try
            {
                if (!System.IO.File.Exists(sFilePath))
                {
                    Console.WriteLine("Image Not in Cache: " + sFilePath);

                    EbFileCategory category = request.ImageInfo.FileCategory;

                    string Qry = this.EbConnectionFactory.DataDB.EB_DOWNLOAD_IMAGE_BY_ID;

                    DbParameter[] parameters = { this.EbConnectionFactory.DataDB.GetNewParameter("fileref", EbDbTypes.Int32, request.ImageInfo.FileRefId) };
                    EbDataTable   t          = this.EbConnectionFactory.DataDB.DoQuery(Qry, parameters);

                    if (t.Rows.Count == 0)
                    {
                        throw new Exception("filestore_sid not found - FileRefId:" + request.ImageInfo.FileRefId + " Quality:" + request.ImageInfo.ImageQuality);
                    }

                    Dictionary <int, ImageMeta> sidAll = new Dictionary <int, ImageMeta>();

                    for (int i = 0; i < t.Rows.Count; i++)
                    {
                        sidAll.Add(Convert.ToInt32(t.Rows[i][0]), new ImageMeta()
                        {
                            FileStoreId = t.Rows[i][1].ToString(), InfraConID = Convert.ToInt32(t.Rows[i][2])
                        });
                    }
                    if (sidAll.ContainsKey((int)request.ImageInfo.ImageQuality))
                    {
                        request.ImageInfo.FileStoreId = sidAll[(int)request.ImageInfo.ImageQuality].FileStoreId;
                        request.ImageInfo.InfraConID  = sidAll[(int)request.ImageInfo.ImageQuality].InfraConID;
                    }
                    else
                    {
                        request.ImageInfo.FileStoreId = sidAll[(int)ImageQuality.original].FileStoreId;
                        request.ImageInfo.InfraConID  = sidAll[(int)ImageQuality.original].InfraConID;
                    }

                    fb = this.EbConnectionFactory.FilesDB.DownloadFileById(request.ImageInfo.FileStoreId, category, request.ImageInfo.InfraConID);

                    if (fb != null)
                    {
                        EbFile.Bytea_ToFile(fb, sFilePath);
                    }
                    else
                    {
                        Console.WriteLine("No File Found in : FileStoreId: " + request.ImageInfo.FileStoreId);
                    }
                }

                if (File.Exists(sFilePath))
                {
                    ms = new MemoryStream(File.ReadAllBytes(sFilePath));

                    dfs.StreamWrapper = new MemorystreamWrapper(ms);
                    dfs.FileDetails   = new FileMeta
                    {
                        FileName           = request.ImageInfo.FileName,
                        FileType           = request.ImageInfo.FileType,
                        Length             = request.ImageInfo.Length,
                        FileStoreId        = request.ImageInfo.FileStoreId,
                        UploadDateTime     = request.ImageInfo.UploadDateTime,
                        MetaDataDictionary = (request.ImageInfo.MetaDataDictionary != null) ? request.ImageInfo.MetaDataDictionary : new Dictionary <String, List <string> >()
                        {
                        },
                    };
                }
                else
                {
                    throw new Exception("File Not Found");
                }
            }
            catch (FormatException e)
            {
                Console.WriteLine("ObjectId not in Correct Format: " + request.ImageInfo.FileName);
                Console.WriteLine("Exception: " + e.ToString());
            }
            catch (Exception e)
            {
                Log.Info("Exception:" + e.ToString());
            }

            return(dfs);
        }