mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.3 KiB
72 lines
2.3 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using Squidex.ClientLibrary.Management;
|
|
|
|
namespace TestSuite.Fixtures
|
|
{
|
|
public class AssetFixture : CreatedAppFixture
|
|
{
|
|
public IAssetsClient Assets => Squidex.Assets;
|
|
|
|
public async Task<MemoryStream> DownloadAsync(AssetDto asset)
|
|
{
|
|
var temp = new MemoryStream();
|
|
|
|
using (var client = new HttpClient())
|
|
{
|
|
var url = $"{ServerUrl}{asset._links["content"].Href}";
|
|
|
|
var response = await client.GetAsync(url);
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
using (var stream = await response.Content.ReadAsStreamAsync())
|
|
{
|
|
await stream.CopyToAsync(temp);
|
|
}
|
|
}
|
|
|
|
return temp;
|
|
}
|
|
|
|
public async Task<AssetDto> UploadFileAsync(string path, AssetDto asset, string fileName = null)
|
|
{
|
|
var fileInfo = new FileInfo(path);
|
|
|
|
using (var stream = fileInfo.OpenRead())
|
|
{
|
|
var upload = new FileParameter(stream, fileName ?? RandomName(fileInfo), asset.MimeType);
|
|
|
|
return await Assets.PutAssetContentAsync(AppName, asset.Id.ToString(), upload);
|
|
}
|
|
}
|
|
|
|
public async Task<AssetDto> UploadFileAsync(string path, string mimeType, string fileName = null, Guid? parentId = null)
|
|
{
|
|
var fileInfo = new FileInfo(path);
|
|
|
|
using (var stream = fileInfo.OpenRead())
|
|
{
|
|
var upload = new FileParameter(stream, fileName ?? RandomName(fileInfo), mimeType);
|
|
|
|
return await Assets.PostAssetAsync(AppName, upload, parentId);
|
|
}
|
|
}
|
|
|
|
private static string RandomName(FileInfo fileInfo)
|
|
{
|
|
var fileName = $"{Guid.NewGuid()}{fileInfo.Extension}";
|
|
|
|
return fileName;
|
|
}
|
|
}
|
|
}
|
|
|