Esempio n. 1
0
        public ActionResult Create(Models.File p, HttpPostedFileBase file)
        {
            //upload image related to product on the bucket
            try
            {
                string link     = "";
                var    filename = "";
                if (file != null)
                {
                    #region Uploading file on Cloud Storage
                    var storage = StorageClient.Create();
                    using (var f = file.InputStream)
                    {
                        filename = Guid.NewGuid() + System.IO.Path.GetExtension(file.FileName);
                        var storageObject = storage.UploadObject("programming-for-the-cloud", filename, null, f);
                        //link = storageObject.MediaLink;
                        link = "https://storage.cloud.google.com/programming-for-the-cloud" + "/" + filename;

                        if (null == storageObject.Acl)
                        {
                            storageObject.Acl = new List <ObjectAccessControl>();
                        }


                        storageObject.Acl.Add(new ObjectAccessControl()
                        {
                            Bucket = "programming-for-the-cloud",
                            Entity = $"user-" + "*****@*****.**",
                            Role   = "READER", //READER
                        });

                        var updatedObject = storage.UpdateObject(storageObject, new UpdateObjectOptions()
                        {
                            // Avoid race conditions.
                            IfMetagenerationMatch = storageObject.Metageneration,
                        });
                    }
                    //store details in a relational db including the filename/link
                    #endregion
                }
                #region Storing details of product in db [INCOMPLETE]
                p.OwnerFk = User.Identity.Name; //"*****@*****.**";

                ServiceAccountCredential credential;
                using (var stream = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "//jurgen-cloud-project-5f077f2e1ba1.json", FileMode.Open, FileAccess.Read))
                {
                    credential = GoogleCredential.FromStream(stream).UnderlyingCredential as ServiceAccountCredential;
                }

                UrlSigner urlSigner = UrlSigner.FromServiceAccountCredential(credential); //https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Storage.V1/index.html
                string    signedUrl = urlSigner.Sign("programming-for-the-cloud", filename, TimeSpan.FromDays(365));

                p.Link = signedUrl;

                //p.Link = link;
                FilesRepository pr = new FilesRepository();
                pr.AddFile(p.Name, p.Description, p.OwnerFk, p.Link);
                #endregion

                #region Updating Cache with latest list of Products from db
                //enable: after you switch on db
                CacheRepository cr = new CacheRepository();
                cr.UpdateCache(pr.GetFiles(User.Identity.Name));
                #endregion

                new LoggingRepository().Logging("File Uploaded By: " + User.Identity.Name + " At: " + DateTime.Now);

                //PubSubRepository psr = new PubSubRepository();
                //psr.AddToEmailQueue(p); //adding it to queue to be sent as an email later on.
                //ViewBag.Message = "Product created successfully";
            }
            catch (Exception ex)
            {
                new LoggingRepository().ErrorLogging(ex);
                ViewBag.Error = "Product failed to be created; " + ex.Message;
            }

            return(RedirectToAction("Index"));
        }