Beispiel #1
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //Cloudinary initialization
            var settings = ConfigurationManager.AppSettings;
            var configuration = new AccountConfiguration(settings["Cloudinary.CloudName"],
                                                         settings["Cloudinary.ApiKey"],
                                                         settings["Cloudinary.ApiSecret"]);

            //Fix issue related to manually generated key for Route.RouteId
            //http://stackoverflow.com/questions/12305784/dataannotation-for-required-property
            GlobalConfiguration.Configuration.Services.RemoveAll(
            typeof(System.Web.Http.Validation.ModelValidatorProvider),
            v => v is InvalidModelValidatorProvider);

            AccountConfiguration.Initialize(configuration);
        }
        private void uploadToCloud(string filename, Stream fileStream)
        {
            if (filename.EndsWith("png") || filename.EndsWith("jpeg") || 
                filename.EndsWith("jpg") || filename.EndsWith("gif") || filename.EndsWith("bmp"))
            {
                try
                {
                    var configuration = new AccountConfiguration("hscl3sr21", "773858917884263", "RWVBnZhCDPyOrKAYihbubppmZ4E");

                    var uploader = new Uploader(configuration);
                    string publicId = Path.GetFileNameWithoutExtension(filename);
                    var uploadResult = uploader.Upload(new UploadInformation(filename, fileStream)
                    {
                        PublicId = publicId,
                        Format = filename.Substring(filename.Length - 3),
                    });
                }
                catch (Exception ex)
                {
                    //context.Response.Write("{ 'success': " + ex.Message + " }");
                    return;
                }
            }
        }
 /// <summary>
 /// Sets the default configuration that can be used from anywhere
 /// in the application. This will also be the configuration used 
 /// by the HtmlHelper and UrlHelper extensions
 /// </summary>
 /// <param name="defaultConfiguration"></param>
 public static void Initialize(AccountConfiguration defaultConfiguration)
 {
     _defaultConfiguration = defaultConfiguration;
 }
        private void uploadToCloud(string filename, string uploadFileName, Stream fileStream, Message msg, Channel chan, string file)
        {
            if (filename.EndsWith("png") || filename.EndsWith("jpeg") || filename.EndsWith("jpg") || filename.EndsWith("gif") || filename.EndsWith("bmp"))
            {
                try
                {
                    msg.Type = MessageTypes.Image;
                    var configuration = new AccountConfiguration("saykor", "277334748579534", "mUjzZ-X3jOuNKGswrAjocB-D-Rc");

                    var uploader = new Uploader(configuration);
                    string publicId = Path.GetFileNameWithoutExtension(filename);
                    var uploadResult = uploader.Upload(new UploadInformation(filename, fileStream)
                    {
                        PublicId = publicId,
                        Format = filename.Substring(filename.Length - 3),
                    });
                    //msg.Content = uploadResult.Url;
                    msg.Content = filename;
                }
                catch (Exception ex)
                {
                    //context.Response.Write("{ 'success': " + ex.Message + " }");
                    return;
                }
            }
            else
            {
                //upload to dropbox
                string cloudPath = "/" + chan.Name + "/" + filename;
                _sessionState.AuthClient();
                var result = _sessionState.DropboxClient.UploadFileAsync(new FileResource(file), cloudPath).Result;
                msg.Content = cloudPath;
            }
        }
Beispiel #5
0
 private string UploadToCloudinary(HttpPostedFileBase file, string filename)
 {
     var settings = ConfigurationManager.AppSettings;
     var configuration = new AccountConfiguration(settings["Cloudinary.CloudName"],
                                                  settings["Cloudinary.ApiKey"],
                                                  settings["Cloudinary.ApiSecret"]);
     var uploader = new Uploader(configuration);
     string publicId = Path.GetFileNameWithoutExtension(filename);
     // Upload the file
     var destroyResult = uploader.Destroy(publicId);
     var uploadResult = uploader.Upload(new UploadInformation(filename, file.InputStream)
                         {
                             // explicitly specify a public id (optional)
                             PublicId = publicId,
                             // set the format, (default is jpg)
                             Format = "png",
                             // Specify some eager transformations
                             Eager = new[]
                             {
                                 new Transformation(100, 100) { Format = "png", Crop = CropMode.Thumb, Gravity = Gravity.Face, Radius = 8 }, //, Angle = new Angle(90)
                             }
                         });
     return uploadResult.Version;
 }
        private void uploadToCloud(string filename, string uploadFileName, HttpContext context, Message msg, Channel chan, string file)
        {
            if (filename.EndsWith("png") || filename.EndsWith("jpeg") || filename.EndsWith("jpg") || filename.EndsWith("gif") || filename.EndsWith("bmp"))
            {
                try
                {
                    msg.PublicData.Type = MessageTypes.Image;
                    var configuration = new AccountConfiguration("saykor", "277334748579534", "mUjzZ-X3jOuNKGswrAjocB-D-Rc");

                    var uploader = new Uploader(configuration);
                    string publicId = Path.GetFileNameWithoutExtension(filename);
                    Stream stream;
                    if (string.IsNullOrEmpty(uploadFileName) == true) // IE Browsers
                        stream = context.Request.Files[0].InputStream;
                    else // Other Browsers
                        stream = context.Request.InputStream;

                    var uploadResult = uploader.Upload(new UploadInformation(filename, stream)
                    {
                        PublicId = publicId,
                        Format = filename.Substring(filename.Length - 3),
                    });
                    msg.PublicData.Value = filename;
                }
                catch (Exception ex)
                {
                    context.Response.Write("{ 'success': " + ex.Message + " }");
                    return;
                }
            }
            else
            {
                //upload to dropbox
                string cloudPath = "/" + chan.Name + "/" + filename;
                _sessionState.AuthClient();
                var result = _sessionState.Client.UploadFileAsync(new FileResource(file), cloudPath).Result;
                msg.PublicData.Value = cloudPath;
            }
        }
Beispiel #7
0
        public HttpResponseMessage UploadAvatar()
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                var httpRequest = HttpContext.Current.Request;

                var context = new GameContext();
                var dbUser = UserPersister.GetUserByUsernameAndDisplayName(httpRequest.Form["username"], httpRequest.Form["nickname"], context);
                if (dbUser == null)
                {
                    throw new InvalidOperationException("This user already exists in the database");
                }

                if (httpRequest.Files.Count > 0)
                {
                    foreach (string file in httpRequest.Files)
                    {
                        var postedFile = httpRequest.Files[file];

                        var configuration = new AccountConfiguration("djlwcsyiz", "781383948985498", "Vh5BQmeTxvSKvTGTg-wRDYKqPz4");

                        var uploader = new Uploader(configuration);
                        string publicId = Path.GetFileNameWithoutExtension(postedFile.FileName);
                        var uploadResult = uploader.Upload(new UploadInformation(postedFile.FileName, postedFile.InputStream)
                        {
                            PublicId = publicId,
                            Format = postedFile.FileName.Substring(postedFile.FileName.Length - 3),
                        });

                        dbUser.Avatar = uploadResult.Url;
                    }
                }

                context.SaveChanges();

                var response = this.Request.CreateResponse(HttpStatusCode.NoContent);
                return response;
            });
        }