public VimeoOrchardResponse()
 {
     this.ErrorCode        = VimeoOrchardErrorCode.GenericError;
     this.Success          = false;
     this.Message          = "Generic Error";
     this.ResolutionAction = VimeoResolutionAction.NoAction;
 }
        public ActionResult FinishUpload(int mediaPartId)
        {
            string message = "";
            VimeoOrchardErrorCode eCode   = VimeoOrchardErrorCode.GenericError;
            VimeoResolutionAction rAction = VimeoResolutionAction.NoAction;
            bool   success   = false;
            string uploadUrl = null; //I use this in case we need to resume an upload

            //re-verify upload
            try {
                switch (_vimeoUploadServices.VerifyUpload(mediaPartId))
                {
                case VerifyUploadResult.CompletedAlready:
                    //the periodic task had already verified that the upload had completed
                    message = T("The upload process has finished.").ToString();
                    eCode   = VimeoOrchardErrorCode.NoError;
                    rAction = VimeoResolutionAction.NoAction;
                    success = true;
                    break;

                case VerifyUploadResult.Complete:
                    //we just found out that the upload is complete
                    try {
                        if (_vimeoUploadServices.TerminateUpload(mediaPartId))
                        {
                            //Make sure the finisher task exists
                            message = T("The upload process has finished.").ToString();
                            eCode   = VimeoOrchardErrorCode.NoError;
                            rAction = VimeoResolutionAction.NoAction;
                            success = true;
                        }
                        else
                        {
                            //we might end up here in the case when the termination was requested at the same time from here and from the task
                            message = T("The upload has completed, but there was an error while handling the finishing touches.").ToString();
                            eCode   = VimeoOrchardErrorCode.FinishingErrors;
                            rAction = VimeoResolutionAction.NoAction;
                            success = false;
                        }
                    } catch (Exception) {
                        //we might end up here in the case when the termination was requested at the same time from here and from the task
                        message = T("The upload has completed, but there was an error while handling the finishing touches.").ToString();
                        eCode   = VimeoOrchardErrorCode.FinishingErrors;
                        rAction = VimeoResolutionAction.NoAction;
                        success = false;
                    }
                    break;

                case VerifyUploadResult.Incomplete:
                    //the upload is still going on
                    message = T("The upload is still in progress.").ToString();
                    eCode   = VimeoOrchardErrorCode.InProgress;
                    rAction = VimeoResolutionAction.ContinueUpload;
                    success = false;
                    //the client may want to resume the upload, so we send them the upload Url
                    uploadUrl = _vimeoUploadServices.GetUploadUrl(mediaPartId);
                    break;

                case VerifyUploadResult.StillUploading:
                    //the upload is still going on
                    message = T("The upload is still in progress.").ToString();
                    eCode   = VimeoOrchardErrorCode.InProgress;
                    rAction = VimeoResolutionAction.ContinueUpload;
                    success = false;
                    //the client may want to resume the upload, so we send them the upload Url
                    uploadUrl = _vimeoUploadServices.GetUploadUrl(mediaPartId);
                    break;

                case VerifyUploadResult.NeverExisted:
                    //we never started an upload with the given Id
                    message = T("The upload was never started, or the MediaPart is not for a Vimeo video.").ToString();
                    eCode   = VimeoOrchardErrorCode.UploadNeverStarted;
                    rAction = VimeoResolutionAction.RestartUpload;
                    success = false;
                    break;

                case VerifyUploadResult.Error:
                    //something went wrong
                    message = T("Unknown error.").ToString();
                    eCode   = VimeoOrchardErrorCode.GenericError;
                    rAction = VimeoResolutionAction.RestartUpload;
                    success = false;
                    break;

                default:
                    //we should never be here
                    message = T("Unknown error.").ToString();
                    eCode   = VimeoOrchardErrorCode.GenericError;
                    rAction = VimeoResolutionAction.RestartUpload;
                    success = false;
                    break;
                }
            } catch (VimeoRateException vre) {
                return(Json(new VimeoOrchardResponse {
                    ErrorCode = VimeoOrchardErrorCode.RateLimited,
                    Success = false,
                    Message = string.Format("Rate will reset on {0} UTC", vre.resetTime.Value.ToString()),
                    ResolutionAction = VimeoResolutionAction.NoAction
                }));
            }

            var response = new VimeoOrchardResponse {
                ErrorCode        = eCode,
                Success          = success,
                Message          = message,
                ResolutionAction = rAction
            };
            //There are issues with using ajax to get the return value from this method:
            // response is a VimeoOrchardResponse, that overrides the ErrorCode field from the Response class.
            // Ajax sees the ErrorCode value from the base object, rather than the correct one. As a fix, we
            // put the error code also in the Data object. Ajax will read that rather than the correct field.
            var ErrorCode = eCode;

            if (!string.IsNullOrWhiteSpace(uploadUrl))
            {
                response.Data = new { uploadUrl, ErrorCode };
            }
            else
            {
                response.Data = new { ErrorCode };
            }

            //return Json(new { ErrorCode = eCode, Success = success, Message = message, Data = response.Data }); //this breaks the api controller, so no
            return(Json(response));
        }