void OnGetContext( IAsyncResult result )
		{
			if ( !Listener.IsListening ) return;

			Listener.BeginGetContext( OnGetContext, null );
			var context = Listener.EndGetContext(result);

			var acs = GetAuth(context);

			AllLogs logs;
			lock( Listener ) logs = _Logs;
			
			// Handle special cases:
			var path = context.Request.Url.AbsolutePath.ToLowerInvariant();
			if( !Handlers.ContainsKey(path) )
				path = "/404";

			var args = new HandlerArgs( )
			{
				HttpListenerContext	= context,
				AccessControlStatus	= acs,
				Logs				= logs,
			};

			try {
				Handlers[ path ]( args );
#if !DEBUG
			} catch ( Exception e ) {
				if ( Platform.IsOnUnix ) {
					File.AppendAllText( Paths.ExceptionsTxt, e.ToString() );
				} else if( Debugger.IsAttached ) {
					Debugger.Break();
				}
#endif
			} finally {
				context.Response.Close();
			}
		}
Example #2
0
        public IActionResult Do(byte[] fileContents, HandlerArgs args)
        {
            var o = new ResizeOptions();

            var m = args.Params.TryGetInt32("m", -1);
            var w = args.Params.TryGetInt32("w", -1);
            var h = args.Params.TryGetInt32("h", -1);
            var c = args.Params.TryGetBoolean("c", false);

            var f           = args.Params.TryGetString("f", "png");
            var contentType = "image/png";
            var imageFormat = ImageFormats.Png;

            switch (f.ToLower())
            {
            case "bmp":
                contentType = "image/bmp";
                imageFormat = ImageFormats.Bmp;
                break;

            case "jpg":
            case "jpeg":
                contentType = "image/jpeg";
                imageFormat = ImageFormats.Jpeg;
                break;

            case "gif":
                contentType = "image/gif";
                imageFormat = ImageFormats.Gif;
                break;
            }

            var base64 = args.Params.TryGetBoolean("base64", false);

            if (m > -1 && m < 6)
            {
                o.Mode = (ResizeMode)m;
            }
            if (w > 0 && h > 0)
            {
                o.Size = new SixLabors.Primitives.Size(w, h);
            }
            if (c)
            {
                o.Compand = c;
            }

            using (var image = Image.Load(fileContents))
            {
                image.Mutate(x => x
                             .Resize(o)
                             .Grayscale());

                if (!base64)
                {
                    //var ms = new MemoryStream();
                    //image.Save(ms, imageFormat);

                    //return new FileStreamResult(ms, contentType);

                    //byte[] _fileContents = new byte[ms.Length];
                    //ms.Read(_fileContents, 0, (int)ms.Length);
                    //return new FileContentResult(_fileContents, contentType);

                    //return new FileContentResult(fileContents, "image/jpeg");

                    var dirpath = Path.Combine(AppContext.BaseDirectory, "files");
                    if (!Directory.Exists(dirpath))
                    {
                        Directory.CreateDirectory(dirpath);
                    }
                    var filepath = Path.Combine(dirpath, $"{Guid.NewGuid().ToString()}.image");

                    using (FileStream output = File.OpenWrite(filepath))
                    {
                        image.Save(output, imageFormat);
                    }

                    return(new PhysicalFileResult(filepath, contentType));
                }
                else
                {
                    return(new JsonResult(new
                    {
                        Value = image.ToBase64String(imageFormat)
                    }));
                }
            }
        }
 public void Notify(HandlerArgs handlerArgs)
 {
     _handler1.Notify(handlerArgs);
     _handler2.Notify(handlerArgs);
     _handler3.Notify(handlerArgs);
 }