Exemplo n.º 1
0
        public HttpResponseMessage Image()
        {
            try
            {
                string origFileName = !string.IsNullOrWhiteSpace(HttpContext.Current.Request["origFileName"]) ? HttpContext.Current.Request["origFileName"] : string.Empty;

                //if true then the file upload is done and we can rename the file.
                if (!string.IsNullOrWhiteSpace(origFileName))
                {
                    string OriginalFileExtension = Path.GetExtension(origFileName);

                    string NewFileName = Guid.NewGuid().ToString().ToUpper() + OriginalFileExtension;

                    string FullAmazonS3URL = CM.AppSettings["AWSUrl"] + CM.AppSettings["AWSBucketName"] + "/" + CM.AppSettings["AWSBucketFolderPath"];

                    CCAWS.Rename.RenameFile(origFileName, NewFileName);

                    CCAWS.ImageUtil.CopyAndResizeImage(NewFileName, NewFileName.Replace(OriginalFileExtension, "") + "thumb" + OriginalFileExtension, 75, 75);

                    Chimera.Entities.Uploads.Image Image = new Chimera.Entities.Uploads.Image();

                    Image.Id = ObjectId.GenerateNewId();
                    Image.ModifiedDateUTC = DateTime.UtcNow;
                    Image.FileName        = System.IO.Path.GetFileNameWithoutExtension(NewFileName);
                    Image.FileExtension   = OriginalFileExtension;
                    Image.Url             = FullAmazonS3URL;

                    if (Chimera.DataAccess.ImageDAO.Save(Image))
                    {
                        HttpContext.Current.Response.ContentType = "text/plain";

                        HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(Image));
                        HttpContext.Current.Response.StatusCode = 200;

                        return(new HttpResponseMessage(HttpStatusCode.OK));
                    }
                }
                //else continue saving the data
                else
                {
                    HttpPostedFile file = HttpContext.Current.Request.Files[0];

                    CCAWS.Upload.UploadFile(file.InputStream, file.FileName);

                    HttpContext.Current.Response.ContentType = "text/plain";
                    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    var result     = new { name = file.FileName };

                    HttpContext.Current.Response.Write(serializer.Serialize(result));
                    HttpContext.Current.Response.StatusCode = 200;

                    return(new HttpResponseMessage(HttpStatusCode.OK));
                }
            }
            catch (Exception e)
            {
                CompanyCommons.Logging.WriteLog("ChimeraWebsite.Admin.UploadController.Image(): ", e);
            }

            return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
        }
Exemplo n.º 2
0
        public HttpResponseMessage Image()
        {
            try
            {
                string origFileName = !string.IsNullOrWhiteSpace(HttpContext.Current.Request["origFileName"]) ? HttpContext.Current.Request["origFileName"] : string.Empty;

                //if true then the file upload is done and we can rename the file.
                if (!string.IsNullOrWhiteSpace(origFileName))
                {
                    string OriginalFileExtension = Path.GetExtension(origFileName);

                    string NewFileName = Guid.NewGuid().ToString().ToUpper() + OriginalFileExtension;

                    string FullAmazonS3URL = CM.AppSettings["AWSUrl"] + CM.AppSettings["AWSBucketName"] + "/" + CM.AppSettings["AWSBucketFolderPath"];

                    CCAWS.Rename.RenameFile(origFileName, NewFileName);

                    CCAWS.ImageUtil.CopyAndResizeImage(NewFileName, NewFileName.Replace(OriginalFileExtension, "") + "thumb" + OriginalFileExtension, 75, 75);

                    Chimera.Entities.Uploads.Image Image = new Chimera.Entities.Uploads.Image();

                    Image.Id = ObjectId.GenerateNewId();
                    Image.ModifiedDateUTC = DateTime.UtcNow;
                    Image.FileName = System.IO.Path.GetFileNameWithoutExtension(NewFileName);
                    Image.FileExtension = OriginalFileExtension;
                    Image.Url = FullAmazonS3URL;

                    if (Chimera.DataAccess.ImageDAO.Save(Image))
                    {
                        HttpContext.Current.Response.ContentType = "text/plain";

                        HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(Image));
                        HttpContext.Current.Response.StatusCode = 200;

                        return new HttpResponseMessage(HttpStatusCode.OK);
                    }
                }
                //else continue saving the data
                else
                {

                    HttpPostedFile file = HttpContext.Current.Request.Files[0];

                    CCAWS.Upload.UploadFile(file.InputStream, file.FileName);

                    HttpContext.Current.Response.ContentType = "text/plain";
                    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    var result = new { name = file.FileName };

                    HttpContext.Current.Response.Write(serializer.Serialize(result));
                    HttpContext.Current.Response.StatusCode = 200;

                    return new HttpResponseMessage(HttpStatusCode.OK);
                }
            }
            catch (Exception e)
            {
                CompanyCommons.Logging.WriteLog("ChimeraWebsite.Admin.UploadController.Image(): ", e);
            }

            return new HttpResponseMessage(HttpStatusCode.InternalServerError);
        }