Exemplo n.º 1
0
        public Stream GetDataStream(byte datFile = 0)
        {
            var thread = Thread.CurrentThread;

            var key = Tuple.Create(thread, datFile);
            WeakReference <Stream> streamRef;

            lock (_DataStreams)
                _DataStreams.TryGetValue(key, out streamRef);

            if (streamRef == null || !streamRef.TryGetTarget(out var stream))
            {
                stream = null;
            }

            if (stream != null)
            {
                return(stream);
            }

            var baseName = String.Format(DatFileFormat, Id.TypeKey, Id.ExpansionKey, Id.Number, datFile);
            var fullPath = Path.Combine(DataDirectory.FullName, Id.Expansion, baseName);


            if (KeepInMemory)
            {
                if (!_Buffers.ContainsKey(datFile))
                {
                    _Buffers.Add(datFile, IOFile.ReadAllBytes(fullPath));
                }
                stream = new MemoryStream(_Buffers[datFile], false);
            }
            else
            {
                stream = IOFile.OpenRead(fullPath);
            }

            lock (_DataStreams) {
                if (_DataStreams.ContainsKey(key))
                {
                    _DataStreams[key].SetTarget(stream);
                }
                else
                {
                    _DataStreams.Add(key, new WeakReference <Stream>(stream));
                }
            }

            return(stream);
        }
Exemplo n.º 2
0
        public ActionResult ObtenirPublication(int?id)
        {
            PUBLICATION pub = null;

            try
            {
                if (id == null)
                {
                    throw new ArgumentNullException("id");
                }

                pub = db.GetPublication(id).First();
            }
            catch
            {
                TempData[Constantes.CLE_MSG_RETOUR] = new Message(Message.TYPE_MESSAGE.ERREUR, Resources.Publication.idPublicationInvalide);
            }

            if (pub != null)
            {
                if (User.IsInRole("admin") || ((List <SECTEUR>)Session["lstSect"]).Any(s => s.ID == pub.IDSECTEUR))
                {
                    var cd = new ContentDisposition {
                        FileName = pub.NOMFICHIERORIGINAL, Inline = false
                    };
                    Response.AppendHeader("Content-Disposition", cd.ToString());

                    try
                    {
                        return(File(IOFile.ReadAllBytes(Fichiers.CheminEnvois(pub.NOMFICHIERSERVEUR)), pub.NOMFICHIERORIGINAL));
                    }
                    catch (IOException)
                    {
                        TempData[Constantes.CLE_MSG_RETOUR] = new Message(Message.TYPE_MESSAGE.ERREUR, Resources.Publication.publicationErreurFichier);
                    }
                }
                else
                {
                    TempData[Constantes.CLE_MSG_RETOUR] = new Message(Message.TYPE_MESSAGE.ERREUR, Resources.Publication.accesRefuse);
                }
            }

            return(RedirectToAction("Publications", "Sectoriel"));
        }
Exemplo n.º 3
0
        public ActionResult Html2Png(int width, int height, string html)
        {
            // fit to max 1920 x 1080.
            if (width > 1920)
            {
                var scale = 1920.0 / width;
                width  = 1920;
                height = (int)(height * scale);
            }
            if (height > 1080)
            {
                var scale = 1080.0 / height;
                width  = (int)(width * scale);
                height = 1080;
            }

            // Realize html string to file.
            var htmlFilePath = Server.MapPath("~/App_Data/" + Guid.NewGuid().ToString("N") + ".html");

            IOFile.WriteAllText(htmlFilePath, html);

            // Prepare for save image file.
            var imageFilePath = Server.MapPath("~/App_Data/" + Guid.NewGuid().ToString("N") + ".png");
            var imageBytes    = default(byte[]);

            try
            {
                // Launch PhantomJS and take a screen shot into image file.
                var procInfo = new ProcessStartInfo
                {
                    FileName  = Server.MapPath("~/bin/phantomjs.exe"),
                    Arguments = string.Format("\"{0}\" {1} {2} \"{3}\" \"{4}\"",
                                              Server.MapPath("~/bin/take-screen-shot.js"),
                                              width,
                                              height,
                                              htmlFilePath,
                                              imageFilePath)
                };
                var proc = Process.Start(procInfo);
                proc.WaitForExit();
            }
            finally
            {
                // Sweep the html file.
                if (IOFile.Exists(htmlFilePath))
                {
                    try { IOFile.Delete(htmlFilePath); }
                    catch (Exception)
                    {
                        // TODO: Report
                    }
                }

                // Read image file into memory and sweep the image file.
                if (IOFile.Exists(imageFilePath))
                {
                    try
                    {
                        imageBytes = IOFile.ReadAllBytes(imageFilePath);
                        IOFile.Delete(imageFilePath);
                    }
                    catch (Exception)
                    {
                        // TODO: Report
                    }
                }
            }

            // Respond to client.
            if (imageBytes != null)
            {
                return(new FileContentResult(imageBytes, "image/png"));
            }
            else
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
            }
        }