internal int?GetCurrentUserId()
 {
     try
     {
         var email = EPiServer.Personalization.EPiServerProfile.Current.Email;
         if (string.IsNullOrEmpty(email))
         {
             return(null);
         }
         var users = TwentyThreeVideoRepository.SearchUsers(email);
         if (users == null || users.Count == 0 || users.Count > 1)
         {
             return(null);
         }
         return(users.First().UserId);
     }
     catch
     {
         return(null);
     }
 }
        public override ContentReference Save(IContent content, SaveAction action)
        {
            var helper        = new VideoHelper();
            int?currentUserId = GetCurrentUserId();
            var newVersion    = content as Video;

            if (content.ContentLink.WorkID != 0)
            {
                if (action == SaveAction.Publish && newVersion != null)
                {
                    TwentyThreeVideoRepository.UpdateVideo(new Photo()
                    {
                        PhotoId = Convert.ToInt32(newVersion.Id),
                        Title   = newVersion.Name,
                        UserId  = currentUserId
                    });
                    newVersion.ContentLink = new ContentReference(int.Parse(newVersion.Id), 0, ProviderKey);
                    _intermediateVideoDataRepository.Update(newVersion);
                    VideoSynchronizationEventHandler.DataStoreUpdated();
                    return(newVersion.ContentLink);
                }

                //If we saved an item that already has a specific version, just return the current version...
                _items.RemoveAt(_items.FindIndex(x => x.ContentLink.Equals(newVersion.ContentLink)));
                _items.Add(newVersion);
                return(content.ContentLink);
            }
            // ...otherwise save the content (which has been made a copy of the UI) to the local repository.
            if (newVersion != null)
            {
                newVersion.Status      = VersionStatus.CheckedOut;
                newVersion.ContentLink = new ContentReference(int.Parse(newVersion.Id), 1, ProviderKey);
            }
            // New content
            else
            {
                var mediaData = content as MediaData;
                if (mediaData != null && mediaData.BinaryData != null)
                {
                    Blob blobData = ((MediaData)content).BinaryData;
                    try
                    {
                        int?videoId = TwentyThreeVideoRepository.UploadVideo(content.Name, blobData, content.ParentLink.ID, currentUserId);
                        if (videoId != null)
                        {
                            var video = GetDefaultContent(LoadContent(content.ParentLink, LanguageSelector.AutoDetect()),
                                                          _contentTypeRepository.Load <Video>().ID, LanguageSelector.AutoDetect()) as Video;
                            var item = TwentyThreeVideoRepository.GetVideo((int)videoId);
                            helper.PopulateVideo(video, item);
                            _items.Add(video);
                            _intermediateVideoDataRepository.Update(video);
                            return(video.ContentLink);
                        }
                    }
                    catch (Exception ex)
                    {
                        _log.Error("An error occured while uploading and creating video object:" + ex);
                        throw;
                    }
                    finally
                    {
                        ServiceLocator.Current.GetInstance <IBlobFactory>().Delete((content as MediaData).BinaryData.ID);
                    }
                }
            }
            _items.Add(newVersion);
            return(newVersion.ContentLink);
        }