예제 #1
0
        public KrakenResponse Upload(KrakenRequest kr, string fileName, string fileExtension)
        {
            var request = new RestRequest("upload", Method.POST);

            if (kr.File.Length == 0)
            {
                return(new KrakenResponse()
                {
                    Success = false,
                    Error = "File parameter was not provided"
                });
            }

            kr.Auth = _auth;

            request.AddFile("image_bytes", kr.File, fileName + fileExtension);

            kr.File = null;

            string json = JsonConvert.SerializeObject(kr, serializerSettings);

            request.AddParameter("json", json);

            var result = Request(request);

            return(result);
        }
예제 #2
0
        public KrakenResponse Url(KrakenRequest kr)
        {
            var request = new RestRequest("url", Method.POST);

            kr.Auth = _auth;

            string json = JsonConvert.SerializeObject(kr, serializerSettings);

            request.AddParameter("application/json", json, ParameterType.RequestBody);

            var response = Request(request);

            return(response);
        }
예제 #3
0
        public override void ExecuteTask()
        {
            KrakenConfig     _kConfig          = Config.Get <KrakenConfig>();
            LibrariesManager _librariesManager = LibrariesManager.GetManager();

            string key          = _kConfig.APIKey;
            string secret       = _kConfig.APISecret;
            bool   useCallbacks = _kConfig.UseCallbacks;
            bool   useLossy     = _kConfig.LossyOptimization;

            Kraken k = new Kraken(key, secret);

            Album album = _librariesManager.GetAlbum(this.AlbumId);

            var albumProvider = (LibrariesDataProvider)album.Provider;

            var images = album.Images().Where(i => i.Status == ContentLifecycleStatus.Master);

            _itemsCount = images.Count();

            foreach (Image image in images)
            {
                KrakenRequest kr = new KrakenRequest();

                kr.Lossy = useLossy;

                if (_kConfig.UseCallbacks)
                {
                    kr.CallbackUrl = _kConfig.CallbackURL;
                }
                else
                {
                    kr.Wait = true;
                }

                // Pull the Stream of the image from the provider.
                // This saves us from having to care about BlobStorage
                Stream imageData = albumProvider.Download(image);

                // Can't trust the length of Stream. Converting to a MemoryStream
                using (MemoryStream ms = new MemoryStream())
                {
                    imageData.CopyTo(ms);

                    kr.File = ms.ToArray();
                }

                var response = k.Upload(kr, image.Id.ToString(), image.Extension);

                if (_kConfig.UseCallbacks)
                {
                    Global.KrakenCallbackIds.Add(response.Id, album.Id);
                }
                else
                {
                    if (response.Success == false || response.Error != null)
                    {
                        UpdateProgress();
                        continue;
                    }

                    using (var webClient = new WebClient())
                        using (var stream = webClient.OpenRead(response.KrakedUrl))
                        {
                            //Check out the master to get a temp version.
                            Image temp = _librariesManager.Lifecycle.CheckOut(image) as Image;

                            //Make the modifications to the temp version.
                            _librariesManager.Upload(temp, stream, Path.GetExtension(response.KrakedUrl));

                            //Checkin the temp and get the updated master version.
                            //After the check in the temp version is deleted.
                            _librariesManager.Lifecycle.CheckIn(temp);

                            _librariesManager.SaveChanges();

                            // Check to see if this image is already published.
                            // If it is, we need to publish the "Master" to update "Live"
                            if (image.GetWorkflowState() == "Published")
                            {
                                var bag = new Dictionary <string, string>();
                                bag.Add("ContentType", typeof(Image).FullName);
                                WorkflowManager.MessageWorkflow(image.Id, typeof(Image), albumProvider.Name, "Publish", false, bag);
                            }
                        }
                }

                UpdateProgress();
            }
        }