Skip to content

Commit

Permalink
#504: uploaded video saving updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simonas Mikulenas committed Jul 17, 2013
1 parent ec94405 commit cf58deb
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@
<Compile Include="Command\SaveVideos\SaveVideosCommand.cs" />
<Compile Include="Command\SaveVideos\SaveVideosRequest.cs" />
<Compile Include="Command\SaveVideos\SaveVideosResponse.cs" />
<Compile Include="Command\SaveVideo\SaveVideoCommand.cs" />
<Compile Include="Command\SaveVideo\SaveVideoRequest.cs" />
<Compile Include="Command\UplaodVideos\GetVideoUploadCommand.cs" />
<Compile Include="Command\UplaodVideos\GetVideoUploadRequest.cs" />
<Compile Include="Command\ViddlerData\GetViddlerDataForUploadCommand.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;

using BetterCms.Core.Mvc.Commands;
using BetterCms.Module.MediaManager.Models;
using BetterCms.Module.Root.Mvc;
using BetterCms.Module.Viddler.Models;
using BetterCms.Module.Viddler.Services;

namespace BetterCms.Module.Viddler.Command.Videos.SaveVideos
{
internal class SaveVideoCommand : CommandBase, ICommand<SaveVideoRequest, Video>
{
private readonly IViddlerService viddlerService;

public SaveVideoCommand(IViddlerService viddlerService)
{
this.viddlerService = viddlerService;
}

public Video Execute(SaveVideoRequest request)
{
var sesionId = viddlerService.GetSessionId();
var data = viddlerService.GetVideoDetails(sesionId, request.VideoId);
var video = new Video
{
OriginalFileName = data.Title,
OriginalFileExtension = string.Empty,
FileUri = new Uri(viddlerService.GetPlayerUrl(data.Id)),
PublicUrl = viddlerService.GetVideoUrl(data.Id),
Size = data.Length,
IsTemporary = true,
IsUploaded = data.IsReady,
Title = data.Title,
Type = MediaType.Video,
ContentType = MediaContentType.File,
PublishedOn = DateTime.Now,
Description = data.Description,
VideoId = data.Id,
ThumbnailUrl = data.ThumbnailUrl,
};
Repository.Save(video);
UnitOfWork.Commit();
return video;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BetterCms.Module.Viddler.Command.Videos.SaveVideos
{
public class SaveVideoRequest
{
public string VideoId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,96 @@

using BetterCms.Core.Mvc.Commands;
using BetterCms.Module.MediaManager.Models;
using BetterCms.Module.MediaManager.Models.Extensions;
using BetterCms.Module.MediaManager.ViewModels.MediaManager;
using BetterCms.Module.Root.Mvc;
using BetterCms.Module.Viddler.Services;
using BetterCms.Module.Viddler.Models;

namespace BetterCms.Module.Viddler.Command.Videos.SaveVideos
{
internal class SaveVideosCommand : CommandBase, ICommand<SaveVideosRequest, SaveVideosResponse>
{
private readonly IViddlerService viddlerService;

public SaveVideosCommand(IViddlerService viddlerService)
{
this.viddlerService = viddlerService;
}

public SaveVideosResponse Execute(SaveVideosRequest request)
{
var response = new SaveVideosResponse() { SelectedFolderId = request.FolderId };
MediaFolder folder = null;
if (!request.FolderId.HasDefaultValue())
var response = new SaveVideosResponse { SelectedFolderId = request.SelectedFolderId ?? Guid.Empty, ReuploadMediaId = request.ReuploadMediaId };

if (request.UploadedFiles != null && request.UploadedFiles.Count > 0)
{
folder = Repository.AsProxy<MediaFolder>(request.FolderId);
if (folder.IsDeleted)
MediaFolder folder = null;

if (request.SelectedFolderId != null && request.SelectedFolderId.Value != Guid.Empty)
{
response.FolderIsDeleted = true;
return response;
folder = Repository.AsProxy<MediaFolder>(request.SelectedFolderId.Value);
if (folder.IsDeleted)
{
response.FolderIsDeleted = true;
return response;
}
}
}

List<Models.Video> medias = new List<Models.Video>();
UnitOfWork.BeginTransaction();
UnitOfWork.BeginTransaction();

// TODO: implement.
var files = new List<Video>();
if (request.ReuploadMediaId.HasDefaultValue())
{
foreach (var fileId in request.UploadedFiles)
{
if (!fileId.HasDefaultValue())
{
var file = Repository.FirstOrDefault<Video>(fileId);
if (folder != null && (file.Folder == null || file.Folder.Id != folder.Id))
{
file.Folder = folder;
}
file.IsTemporary = false;
file.PublishedOn = DateTime.Now;
Repository.Save(file);
files.Add(file);
}
}
}
else
{
// Re-upload performed.
var fileId = request.UploadedFiles.FirstOrDefault();
if (!fileId.HasDefaultValue())
{
var originalMedia = Repository.First<Video>(request.ReuploadMediaId);
Repository.Save(originalMedia.CreateHistoryItem());

// Title = video.Title,
// IsArchived = false,
// Type = MediaType.Video,
// ContentType = MediaContentType.File,
// Folder = folder,
// PublishedOn = DateTime.Now,
// Description = video.Description,
//
// OriginalFileName = video.Title,
// OriginalFileExtension = null,
// FileUri = new Uri(viddlerService.GetPlayerUrl(video.Id)),
// PublicUrl = viddlerService.GetVideoUrl(video.Id),
// Size = 0,
// IsTemporary = false,
// IsUploaded = video.Is_Transcoding == 0,
// IsCanceled = false,
//
// VideoId = video.Id,
// ThumbnailUrl = video.Thumbnails.GetThumbnailUrl(300, 300),
var file = Repository.FirstOrDefault<Video>(fileId);
file.CopyDataTo(originalMedia);
originalMedia.IsTemporary = false;
originalMedia.PublishedOn = DateTime.Now;
files.Add(originalMedia);
}
}

UnitOfWork.Commit();
UnitOfWork.Commit();

response.Medias =
medias.Select(
file =>
(MediaFileViewModel)
new MediaVideoViewModel
{
Id = file.Id,
Name = file.Title,
Type = file.Type,
Version = file.Version,
ContentType = MediaContentType.File,
PublicUrl = file.PublicUrl,
FileExtension = file.OriginalFileExtension,
IsProcessing = !file.IsUploaded.HasValue,
IsFailed = file.IsUploaded.HasValue && !file.IsUploaded.Value
}).ToList();
// Notify.
foreach (var mediaFile in files)
{
Events.MediaManagerEvents.Instance.OnMediaFileUpdated(mediaFile);
}

response.Medias =
files.Select(
file =>
(MediaFileViewModel)
new MediaVideoViewModel
{
Id = file.Id,
Name = file.Title,
Type = file.Type,
Version = file.Version,
ContentType = MediaContentType.File,
PublicUrl = file.PublicUrl,
FileExtension = file.OriginalFileExtension,
IsProcessing = !file.IsUploaded.HasValue,
IsFailed = file.IsUploaded.HasValue && !file.IsUploaded.Value
}).ToList();
}

return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ namespace BetterCms.Module.Viddler.Command.Videos.SaveVideos
{
public class SaveVideosRequest
{
public Guid FolderId { get; set; }
public List<string> VideosIds { get; set; }
public Guid RootFolderId { get; set; }

public Guid ReuploadMediaId { get; set; }

public Guid? SelectedFolderId { get; set; }

public IList<Guid> UploadedFiles { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public SaveVideosResponse()

public bool FolderIsDeleted { get; set; }

public Guid ReuploadMediaId { get; set; }
}
}
49 changes: 29 additions & 20 deletions Modules/BetterCms.Module.Viddler/Controllers/VideosController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,31 +94,40 @@ public ActionResult ViddlerUploadMockup(string uploadtoken, string callback, str
/// <returns>The response.</returns>
public WrappedJsonResult VideoUploaded(string video_id, string token, string uploadToken)
{
if (HttpRuntime.Cache[token] != null)
// TODO: check return parameter name from Viddler - token or uploadToken?
if (HttpRuntime.Cache[token] == null)
{
HttpRuntime.Cache.Remove(token);
return null;
}
HttpRuntime.Cache.Remove(token);

// TODO: implement callback from Viddler.
var media = new MediaFile() { OriginalFileName = "some file name" };
return new WrappedJsonResult
{
Data = new
var video = GetCommand<SaveVideoCommand>().ExecuteCommand(
new SaveVideoRequest
{
Success = true,
Id = media.Id,
FileName = media.OriginalFileName,
FileSize = media.Size,
Version = media.Version,
Type = MediaType.Video,
IsProcessing = !media.IsUploaded.HasValue,
IsFailed = media.IsUploaded == false,
},
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
VideoId = video_id
});

if (video == null)
{
return null;
}

// Failed.
return null;
return new WrappedJsonResult
{
Data =
new
{
Success = true,
Id = video.Id,
FileName = video.Title,
FileSize = video.Size,
Version = video.Version,
Type = MediaType.Video,
IsProcessing = !video.IsUploaded.HasValue,
IsFailed = video.IsUploaded == false,
},
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public VideosJsModuleIncludeDescriptor(ModuleDescriptor module)
{
Links = new IActionProjection[]
{
new JavaScriptModuleLinkTo<VideosController>(this, "uploadVideoDialogUrl", c => c.UploadVideos(null, null)),
new JavaScriptModuleLinkTo<VideosController>(this, "uploadVideoDialogUrl", c => c.UploadVideos("{0}", "{1}")),
new JavaScriptModuleLinkTo<VideosController>(this, "getUploadDataUrl", c => c.GetViddlerDataForUpload()),
new JavaScriptModuleLinkTo<VideosController>(this, "saveUploadedVideosUrl", c => c.SaveVideos(null)),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,17 +360,17 @@ bettercms.define('bcms.viddler.videos', ['bcms.jquery', 'bcms', 'bcms.dynamicCon
};
}

module.uploadVideo = function(rootFolderId, onSaveCallback, reuploadMediaId) {
module.uploadVideo = function(folderId, onSaveCallback, reuploadMediaId) {
var options = {
uploads: new UploadsViewModel(),
rootFolderId: rootFolderId,
rootFolderId: folderId,
reuploadMediaId: reuploadMediaId
};
options.uploads.filesToAccept('video/*');
modal.open({
title: globalization.uploadFilesDialogTitle,
onLoad: function (dialog) {
var url = $.format(links.uploadVideoDialogUrl, rootFolderId, reuploadMediaId);
var url = $.format(links.uploadVideoDialogUrl, folderId, reuploadMediaId);
dynamicContent.setContentFromUrl(dialog, url, {
done: function () {
VideoUpload(dialog, options);
Expand Down

0 comments on commit cf58deb

Please sign in to comment.