Skip to content

Commit

Permalink
Merge branch 'develop2'
Browse files Browse the repository at this point in the history
  • Loading branch information
yar229 committed Jan 17, 2019
2 parents f7825b4 + cdac1e7 commit 700de52
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 17 deletions.
35 changes: 33 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,45 @@ public CryptoKeyInfo EnsurePublicKey(MailRuCloud cloud)
return ServiceInfo.CryptInfo.PublicKey;
}

public PublishInfo ToPublishInfo()
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});
info.Items.Add(new PublishInfoItem
{
Path = innerFile.FullPath,
Url = ConstSettings.PublishFileLink + innerFile.PublicLink,
PlaylistUrl = !isSplitted || cnt > 0
? generateDirectVideoLink
? ConvertToVideoLink(cloud, innerFile.PublicLink)
: null
: null
});
cnt++;
}

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);
}
}
}

39 changes: 32 additions & 7 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,21 +260,42 @@ 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, 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);
innerFile.PublicLink = url;
}
var info = file.ToPublishInfo();
var info = file.ToPublishInfo(this, generateDirectVideoLink);

if (makeShareFile)
{
string path = $"{file.FullPath}{PublishInfo.SharedFilePostfix}";
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)
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);
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
4 changes: 2 additions & 2 deletions MailRuCloud/MailRuCloudApi/MailRuCloudApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFrameworks>netcoreapp2.0;net452</TargetFrameworks>
<RootNamespace>YaR.MailRuCloud.Api</RootNamespace>
<AssemblyName>MailRuCloud.Api</AssemblyName>
<AssemblyVersion>1.10.2.2</AssemblyVersion>
<FileVersion>1.10.2.2</FileVersion>
<AssemblyVersion>1.10.3.0</AssemblyVersion>
<FileVersion>1.10.3.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 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 All @@ -15,5 +16,6 @@ public class PublishInfoItem
{
public string Path { get; set; }
public string Url { get; set; }
public string PlaylistUrl { get; set; }
}
}
21 changes: 18 additions & 3 deletions MailRuCloud/MailRuCloudApi/SpecialCommands/ShareCommand.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using YaR.MailRuCloud.Api.Base;
using YaR.MailRuCloud.Api.Base.Requests.Types;

namespace YaR.MailRuCloud.Api.SpecialCommands
{
public class ShareCommand : SpecialCommand
{
public ShareCommand(MailRuCloud cloud, string path, 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);

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

await Cloud.Publish(entry);
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,7 +61,17 @@ public class SpecialCommandFabric
new SpecialCommandContainer
{
Commands = new [] {"share"},
CreateFunc = (cloud, path, param) => new ShareCommand(cloud, path, 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, 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
4 changes: 2 additions & 2 deletions WDMRC.Console/WDMRC.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<Copyright>[email protected]</Copyright>
<Description>WebDAV emulator for Cloud.mail.ru</Description>
<PackageId>WebDAVCloudMailRu</PackageId>
<AssemblyVersion>1.10.2.2</AssemblyVersion>
<FileVersion>1.10.2.2</FileVersion>
<AssemblyVersion>1.10.3.0</AssemblyVersion>
<FileVersion>1.10.3.0</FileVersion>
<AssemblyName>wdmrc</AssemblyName>
<RootNamespace>YaR.CloudMailRu.Console</RootNamespace>
<StartupObject></StartupObject>
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 700de52

Please sign in to comment.