Beispiel #1
0
        public Object DownloadFile(String uniqueName)
        {
            //Physical Path of Root Folder
            var rootPath = System.Web.HttpContext.Current.Server.MapPath("~/UploadedFiles");

            //Find File from DB against unique name
            var fileDTO = DummyDAL.GetFileByUniqueID(uniqueName);

            if (fileDTO != null)
            {
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                var fileFullPath             = System.IO.Path.Combine(rootPath, fileDTO.FileUniqueName + fileDTO.FileExt);

                byte[] file = System.IO.File.ReadAllBytes(fileFullPath);
                System.IO.MemoryStream ms = new System.IO.MemoryStream(file);

                response.Content = new ByteArrayContent(file);
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                //String mimeType = MimeType.GetMimeType(file); //You may do your hard coding here based on file extension

                response.Content.Headers.ContentType = new MediaTypeHeaderValue(fileDTO.ContentType);// obj.DocumentName.Substring(obj.DocumentName.LastIndexOf(".") + 1, 3);
                response.Content.Headers.ContentDisposition.FileName = fileDTO.FileActualName;
                return(response);
            }
            else
            {
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NotFound);
                return(response);
            }
        }
        public Object ValidateUser(LoginInfo loginInfo)
        {
            var userObj = DummyDAL.ValidateUser(loginInfo.Login, loginInfo.Password);

            if (userObj != null)
            {
                string key         = "my_secret_key_12345";
                var    issuer      = "http://mysite.com";
                var    securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));
                var    credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

                var permClaims = new List <Claim>();
                permClaims.Add(new Claim("valid", "1"));
                permClaims.Add(new Claim("userid", userObj.UserId.ToString()));
                permClaims.Add(new Claim("name", userObj.Name));

                var token = new JwtSecurityToken(issuer,
                                                 issuer,
                                                 permClaims,
                                                 expires: DateTime.Now.AddDays(1),
                                                 signingCredentials: credentials);
                var jwt_token = new JwtSecurityTokenHandler().WriteToken(token);
                return(new { success = true, Name = userObj.Name, token = jwt_token });
            }
            else
            {
                return(new { success = false });
            }
        }
Beispiel #3
0
    public static void Main()
    {
        dal           = new DummyDAL();
        battleHandler = new BattleHandler();

        Console.WriteLine("Consumer starting");

        // DO STUFF
        var factory = new ConnectionFactory
        {
            HostName           = "localhost",
            Port               = 5672,
            RequestedHeartbeat = 60
        };

        using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                var consumer = new EventingBasicConsumer(channel);

                SetupQueues(channel, consumer);

                consumer.Received += (model, ea) =>
                {
                    var response = string.Empty;

                    var body       = ea.Body;
                    var props      = ea.BasicProperties;
                    var replyProps = channel.CreateBasicProperties();
                    replyProps.CorrelationId = props.CorrelationId;

                    try
                    {
                        response = Handle(ea);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(" [.] " + e.Message);
                        response = "";
                    }
                    finally
                    {
                        var responseBytes = Encoding.UTF8.GetBytes(response);
                        channel.BasicPublish(exchange: "", routingKey: props.ReplyTo, basicProperties: replyProps, body: responseBytes);
                        channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                    }
                };



                Console.ReadLine();
            }
    }
        public Object GetPersons([FromHeader] ClaimDTO claimDto)
        {
            try
            {
                var persons = DummyDAL.GetPersons(claimDto.UserID);

                return(new
                {
                    success = true,
                    data = persons
                });
            }
            catch (Exception ex)
            {
                return(new { success = false });
            }
        }
        public void UploadFile(List <IFormFile> UploadedImage, [FromHeader] ClaimDTO claimDto)
        {
            //var files = HttpContext.Request.Form.Files;
            var age = HttpContext.Request.Form["Age"].FirstOrDefault();

            foreach (var file in UploadedImage)
            {
                FileDTO fileDTO = new FileDTO();

                fileDTO.FileActualName = file.FileName;
                fileDTO.FileExt        = Path.GetExtension(file.FileName);
                fileDTO.ContentType    = file.ContentType;

                //Generate a unique name using Guid
                //fileDTO.FileUniqueName = Guid.NewGuid().ToString();
                //OR
                fileDTO.FileUniqueName = Path.GetRandomFileName();

                //Any data we want to get from claim
                fileDTO.UploadedByID = claimDto.UserID;

                //Get physical path of our folder where we want to save images
                //var rootPath = HttpContext.Current.Server.MapPath("~/UploadedFiles"); // This doesn't work now

                //How to get root path?
                // Hard code root path in configuration?
                //Use IWebHostEnvironment
                var p1 = _env.ContentRootPath;
                var p2 = _env.WebRootPath;

                var fileSavePath = System.IO.Path.Combine(p1, "UploadFiles", fileDTO.FileUniqueName + fileDTO.FileExt);

                // Save the uploaded file to "UploadedFiles" folder
                using (var stream = System.IO.File.Create(fileSavePath))
                {
                    file.CopyTo(stream);
                }

                //Save File Meta data in Database
                DummyDAL.SaveFileInDB(fileDTO);
            }
        }
Beispiel #6
0
        public void UploadFile()
        {
            if (HttpContext.Current.Request.Files.Count > 0)
            {
                try
                {
                    foreach (var fileName in HttpContext.Current.Request.Files.AllKeys)
                    {
                        HttpPostedFile file = HttpContext.Current.Request.Files[fileName];
                        if (file != null)
                        {
                            FileDTO fileDTO = new FileDTO();

                            fileDTO.FileActualName = file.FileName;
                            fileDTO.FileExt        = Path.GetExtension(file.FileName);
                            fileDTO.ContentType    = file.ContentType;

                            //Generate a unique name using Guid
                            fileDTO.FileUniqueName = Guid.NewGuid().ToString();

                            //Get physical path of our folder where we want to save images
                            var rootPath = HttpContext.Current.Server.MapPath("~/UploadedFiles");

                            var fileSavePath = System.IO.Path.Combine(rootPath, fileDTO.FileUniqueName + fileDTO.FileExt);

                            // Save the uploaded file to "UploadedFiles" folder
                            file.SaveAs(fileSavePath);

                            //Save File Meta data in Database

                            DummyDAL.SaveFileInDB(fileDTO);
                        }
                    }//end of foreach
                }
                catch (Exception ex)
                { }
            }//end of if count > 0

            var age = HttpContext.Current.Request["Age"];
        }
        public IActionResult DownloadFile(String uniqueName)
        {
            var fileDTO = DummyDAL.GetFileByUniqueID(uniqueName);

            if (fileDTO != null)
            {
                System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
                {
                    FileName = fileDTO.FileActualName,
                    Inline   = false
                };
                Response.Headers.Add("Content-Disposition", cd.ToString());


                /* //OR We could have done like following
                 * var contentDisposition = new ContentDispositionHeaderValue("attachment");
                 * contentDisposition.SetHttpFileName("FileDownloadName.jpg");
                 * Response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString();
                 */

                //Get Physical Path of Root Folder
                var p1           = _env.ContentRootPath;
                var fileReadPath = System.IO.Path.Combine(p1, "UploadFiles", fileDTO.FileUniqueName + fileDTO.FileExt);


                var image = System.IO.File.OpenRead(fileReadPath);
                return(File(image, fileDTO.ContentType));

                //var image = System.IO.File.OpenRead("D:\\nunit.jpg");
                //return File(image, "image/jpeg");

                //return new PhysicalFile(@"C:\test.jpg", "image/jpeg");
            }
            else
            {
                return(StatusCode(404));
            }
        }
 public Object GetFiles()
 {
     return(new { data = DummyDAL.GetAllFiles() });
 }
Beispiel #9
0
        public ActionResult Index()
        {
            var files = DummyDAL.GetAllFiles();

            return(View(files));
        }