Skip to content

Commit

Permalink
#148 pl command to generate m3u8 playlist file
Browse files Browse the repository at this point in the history
  • Loading branch information
yar229 committed Jan 17, 2019
1 parent ff95640 commit dd291ab
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 13 deletions.
13 changes: 10 additions & 3 deletions MailRuCloud/MailRuCloudApi/Base/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,24 @@ public CryptoKeyInfo EnsurePublicKey(MailRuCloud cloud)
public PublishInfo ToPublishInfo(MailRuCloud cloud, bool generateDirectVideoLink)
{
var info = new PublishInfo();

bool isSplitted = Files.Count > 1;

int cnt = 0;
foreach (var innerFile in Files)
{
if (!string.IsNullOrEmpty(innerFile.PublicLink))
info.Items.Add(new PublishInfoItem
{
Path = innerFile.FullPath,
Url = ConstSettings.PublishFileLink + innerFile.PublicLink,
PlaylistUrl = generateDirectVideoLink
? ConvertToVideoLink(cloud, innerFile.PublicLink)
: null
PlaylistUrl = !isSplitted || cnt > 0
? generateDirectVideoLink
? ConvertToVideoLink(cloud, innerFile.PublicLink)
: null
: null
});
cnt++;
}

return info;
Expand Down
37 changes: 31 additions & 6 deletions MailRuCloud/MailRuCloudApi/MailRuCloud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mime;
using System.Security.Authentication;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -259,8 +260,11 @@ private async Task<string> Publish(string fullPath)
return res.Url;
}

public async Task<PublishInfo> Publish(File file, bool makeShareFile = true, bool generateDirectVideoLink = false)
public async Task<PublishInfo> Publish(File file, bool makeShareFile = true, bool generateDirectVideoLink = false, bool makeM3UFile = false)
{
if (file.Files.Count > 1 && (generateDirectVideoLink || makeM3UFile))
throw new ArgumentException($"Cannot generate direct video link for splitted file {file.FullPath}");

foreach (var innerFile in file.Files)
{
var url = await Publish(innerFile.FullPath);
Expand All @@ -274,6 +278,24 @@ public async Task<PublishInfo> Publish(File file, bool makeShareFile = true, boo
UploadFileJson(path, info)
.ThrowIf(r => !r, r => new Exception($"Cannot upload JSON file, path = {path}"));
}


if (makeM3UFile)
{
string path = $"{file.FullPath}{PublishInfo.PlaylistFilePostfix}";
var content = new StringBuilder();
{
content.Append("#EXTM3U\r\n");
foreach (var item in info.Items)
{
content.Append($"#EXTINF:-1,{WebDavPath.Name(item.Path)}\r\n");
content.Append($"{item.PlaylistUrl}\r\n");
}
}
UploadFile(path, content.ToString())
.ThrowIf(r => !r, r => new Exception($"Cannot upload JSON file, path = {path}"));
}

return info;
}

Expand All @@ -293,12 +315,12 @@ public async Task<PublishInfo> Publish(Folder folder, bool makeShareFile = true)
return info;
}

public async Task<PublishInfo> Publish(IEntry entry, bool makeShareFile = true, bool generateDirectVideoLink = false)
public async Task<PublishInfo> Publish(IEntry entry, bool makeShareFile = true, bool generateDirectVideoLink = false, bool makeM3UFile = false)
{
if (null == entry) throw new ArgumentNullException(nameof(entry));

if (entry is File file)
return await Publish(file, makeShareFile, generateDirectVideoLink);
return await Publish(file, makeShareFile, generateDirectVideoLink, makeM3UFile);
if (entry is Folder folder)
return await Publish(folder, makeShareFile);

Expand Down Expand Up @@ -869,20 +891,23 @@ public async Task<string> DownloadFileAsString(string path)
}


public void UploadFile(string path, byte[] content, bool discardEncryption = false)
public bool UploadFile(string path, byte[] content, bool discardEncryption = false)
{
using (var stream = GetFileUploadStream(path, content.Length, discardEncryption).Result)
{
stream.Write(content, 0, content.Length);
}
_itemCache.Invalidate(path, WebDavPath.Parent(path));

return true;
}


public void UploadFile(string path, string content, bool discardEncryption = false)
public bool UploadFile(string path, string content, bool discardEncryption = false)
{
var data = Encoding.UTF8.GetBytes(content);
UploadFile(path, data, discardEncryption);
return UploadFile(path, data, discardEncryption);

}

public bool UploadFileJson<T>(string fullFilePath, T data, bool discardEncryption = false)
Expand Down
1 change: 1 addition & 0 deletions MailRuCloud/MailRuCloudApi/PublishInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace YaR.MailRuCloud.Api
public class PublishInfo
{
public const string SharedFilePostfix = ".share.wdmrc";
public const string PlaylistFilePostfix = ".m3u8";

public List<PublishInfoItem> Items { get; set; } = new List<PublishInfoItem>();
public DateTime DateTime { get; set; } = DateTime.Now;
Expand Down
17 changes: 15 additions & 2 deletions MailRuCloud/MailRuCloudApi/SpecialCommands/ShareCommand.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using YaR.MailRuCloud.Api.Base;
Expand All @@ -6,12 +7,16 @@ namespace YaR.MailRuCloud.Api.SpecialCommands
{
public class ShareCommand : SpecialCommand
{
public ShareCommand(MailRuCloud cloud, string path, bool generateDirectVideoLink, IList<string> parames) : base(cloud, path, parames)

public ShareCommand(MailRuCloud cloud, string path, bool generateDirectVideoLink, bool makeM3UFile, IList<string> parames) : base(cloud, path, parames)
{
_generateDirectVideoLink = generateDirectVideoLink;
_makeM3UFile = makeM3UFile;
}


private readonly bool _generateDirectVideoLink;
private readonly bool _makeM3UFile;

protected override MinMax<int> MinMaxParamsCount { get; } = new MinMax<int>(0, 1);

Expand All @@ -31,7 +36,15 @@ public override async Task<SpecialCommandResult> Execute()
if (null == entry)
return SpecialCommandResult.Fail;

await Cloud.Publish(entry, true, _generateDirectVideoLink);
try
{
await Cloud.Publish(entry, true, _generateDirectVideoLink, _makeM3UFile);
}
catch (Exception e)
{
return new SpecialCommandResult(false, e.Message);
}

return SpecialCommandResult.Success;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,17 @@ public class SpecialCommandFabric
new SpecialCommandContainer
{
Commands = new [] {"share"},
CreateFunc = (cloud, path, param) => new ShareCommand(cloud, path, false, param)
CreateFunc = (cloud, path, param) => new ShareCommand(cloud, path, false, false, param)
},
new SpecialCommandContainer
{
Commands = new [] {"sharev"},
CreateFunc = (cloud, path, param) => new ShareCommand(cloud, path, true, param)
CreateFunc = (cloud, path, param) => new ShareCommand(cloud, path, true, false, param)
},
new SpecialCommandContainer
{
Commands = new [] {"pl"},
CreateFunc = (cloud, path, param) => new ShareCommand(cloud, path, true, true, param)
},
new SpecialCommandContainer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ public SpecialCommandResult(bool isSuccess)
IsSuccess = isSuccess;
}

public SpecialCommandResult(bool isSuccess, string message) : this(isSuccess)
{
Message = message;
}

public bool IsSuccess { get;}
public string Message { get; }

public static SpecialCommandResult Success => new SpecialCommandResult(true);
public static SpecialCommandResult Fail => new SpecialCommandResult(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ public Task<StoreCollectionResult> CreateCollectionAsync(string name, bool overw
if (cmd != null)
{
var res = cmd.Execute().Result;
if (!res.IsSuccess)
Logger.Log(LogLevel.Error, res.Message);

return Task.FromResult(new StoreCollectionResult(res.IsSuccess ? DavStatusCode.Created : DavStatusCode.PreconditionFailed));
}

Expand Down

0 comments on commit dd291ab

Please sign in to comment.