Skip to content

Commit

Permalink
#148 sharev command to generate direct playlist links
Browse files Browse the repository at this point in the history
  • Loading branch information
yar229 committed Jan 16, 2019
1 parent 2b5ac17 commit 7e9676c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
28 changes: 26 additions & 2 deletions MailRuCloud/MailRuCloudApi/Base/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using YaR.MailRuCloud.Api.Base.Requests.Types;

namespace YaR.MailRuCloud.Api.Base
{
Expand Down Expand Up @@ -163,16 +165,38 @@ public CryptoKeyInfo EnsurePublicKey(MailRuCloud cloud)
return ServiceInfo.CryptInfo.PublicKey;
}

public PublishInfo ToPublishInfo()
public PublishInfo ToPublishInfo(MailRuCloud cloud, bool generateDirectVideoLink)
{
var info = new PublishInfo();
foreach (var innerFile in Files)
{
if (!string.IsNullOrEmpty(innerFile.PublicLink))
info.Items.Add(new PublishInfoItem{Path = innerFile.FullPath, Url = ConstSettings.PublishFileLink + innerFile.PublicLink});
info.Items.Add(new PublishInfoItem
{
Path = innerFile.FullPath,
Url = ConstSettings.PublishFileLink + innerFile.PublicLink,
PlaylistUrl = generateDirectVideoLink
? ConvertToVideoLink(cloud, innerFile.PublicLink)
: null
});
}

return info;
}

private string ConvertToVideoLink(MailRuCloud cloud, string publicLink)
{
return cloud.Account.RequestRepo.GetShardInfo(ShardType.WeblinkVideo).Result.Url +
"0p/" +
Base64Encode(publicLink.TrimStart('/')) +
".m3u8?double_encode=1";
}

private static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return Convert.ToBase64String(plainTextBytes);
}
}
}

8 changes: 4 additions & 4 deletions MailRuCloud/MailRuCloudApi/MailRuCloud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,14 @@ private async Task<string> Publish(string fullPath)
return res.Url;
}

public async Task<PublishInfo> Publish(File file, bool makeShareFile = true)
public async Task<PublishInfo> Publish(File file, bool makeShareFile = true, bool generateDirectVideoLink = false)
{
foreach (var innerFile in file.Files)
{
var url = await Publish(innerFile.FullPath);
innerFile.PublicLink = url;
}
var info = file.ToPublishInfo();
var info = file.ToPublishInfo(this, generateDirectVideoLink);

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

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

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

Expand Down
1 change: 1 addition & 0 deletions MailRuCloud/MailRuCloudApi/PublishInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public class PublishInfoItem
{
public string Path { get; set; }
public string Url { get; set; }
public string PlaylistUrl { get; set; }
}
}
30 changes: 30 additions & 0 deletions MailRuCloud/MailRuCloudApi/SpecialCommands/ShareCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,34 @@ public override async Task<SpecialCommandResult> Execute()
return SpecialCommandResult.Success;
}
}


public class ShareVCommand : SpecialCommand
{
public ShareVCommand(MailRuCloud cloud, string path, IList<string> parames) : base(cloud, path, parames)
{
}

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

public override async Task<SpecialCommandResult> Execute()
{
string path;
string param = Parames.Count == 0 ? string.Empty : Parames[0].Replace("\\", WebDavPath.Separator);

if (Parames.Count == 0)
path = Path;
else if (param.StartsWith(WebDavPath.Separator))
path = param;
else
path = WebDavPath.Combine(Path, param);

var entry = await Cloud.GetItemAsync(path);
if (null == entry)
return SpecialCommandResult.Fail;

await Cloud.Publish(entry, true, true);
return SpecialCommandResult.Success;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public class SpecialCommandFabric
CreateFunc = (cloud, path, param) => new ShareCommand(cloud, path, param)
},
new SpecialCommandContainer
{
Commands = new [] {"sharev"},
CreateFunc = (cloud, path, param) => new ShareVCommand(cloud, path, param)
},
new SpecialCommandContainer
{
Commands = new [] {"test"},
CreateFunc = (cloud, path, param) => new TestCommand(cloud, path, param)
Expand Down

0 comments on commit 7e9676c

Please sign in to comment.