Fix the bug of creating access policy of device icon

1.Fix the bug of 404 Not Found error when the first time setup device icon
container and its policy.
2.Also fix the bug of upload file in schedule device icon job for Chrome
browser, checkout empty file name.
This commit is contained in:
Hua Zhang 2017-01-13 21:50:20 +08:00
Родитель 9b6bca44ea
Коммит 2a69ce02ed
8 изменённых файлов: 38 добавлений и 39 удалений

Просмотреть файл

@ -168,27 +168,21 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.Common.Helpers
return new BlobStorageReader(blobs);
}
public async Task<SharedAccessBlobPolicies> CreateSASPolicyIfNotExist(string policyName, SharedAccessBlobPolicy policy)
public async Task CreateAccessPolicyIfNotExist(BlobContainerPublicAccessType publicAccessType, string policyName, SharedAccessBlobPolicy policy)
{
await CreateCloudBlobContainerAsync();
var currentPermissions = _container.GetPermissions();
var policies = currentPermissions.SharedAccessPolicies;
if (!policies.ContainsKey(policyName))
if (_container == null && _containerName != null)
{
policies.Add(policyName, policy);
_container.SetPermissions(currentPermissions);
_container = _blobClient.GetContainerReference(_containerName);
await _container.CreateIfNotExistsAsync(publicAccessType, null, null);
var currentPermissions = _container.GetPermissions();
var policies = currentPermissions.SharedAccessPolicies;
if (!policies.ContainsKey(policyName))
{
policies.Add(policyName, policy);
_container.SetPermissions(currentPermissions);
}
}
return policies;
}
public async Task SetPublicPolicyType(BlobContainerPublicAccessType accessType)
{
await CreateCloudBlobContainerAsync();
var currentPermissions = _container.GetPermissions();
currentPermissions.PublicAccess = accessType;
_container.SetPermissions(currentPermissions);
}
private async Task CreateCloudBlobContainerAsync()

Просмотреть файл

@ -22,7 +22,6 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.Common.Helpers
Task<string> GetBlobEtag(string blobName);
Task UploadTextAsync(string blobName, string data);
Task<IBlobStorageReader> GetReader(string blobPrefix, DateTime? minTime = null);
Task<SharedAccessBlobPolicies> CreateSASPolicyIfNotExist(string policyName, SharedAccessBlobPolicy policy);
Task SetPublicPolicyType(BlobContainerPublicAccessType accessType);
Task CreateAccessPolicyIfNotExist(BlobContainerPublicAccessType publicAccessType,string policyName, SharedAccessBlobPolicy policy);
}
}

Просмотреть файл

@ -27,12 +27,12 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Infr
_deviceIconsBlobStoreContainerName = configurationProvider.GetConfigurationSettingValueOrDefault("DeviceIconStoreContainerName", "deviceicons");
_blobStorageClient = blobStorageClientFactory.CreateClient(_storageAccountConnectionString, _deviceIconsBlobStoreContainerName);
_writePolicyName = _deviceIconsBlobStoreContainerName + "-write";
_blobStorageClient.SetPublicPolicyType(BlobContainerPublicAccessType.Blob);
CreateSASPolicyIfNotExist();
}
public async Task<DeviceIcon> AddIcon(string fileName, Stream fileStream)
{
await CreateAccessPolicyIfNotExist();
// replace '.' with '_' so that the name can be used in MVC Url route.
var name = Path.GetFileName(fileName).Replace(".", "_");
var extension = Path.GetExtension(fileName);
@ -50,12 +50,16 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Infr
public async Task<DeviceIcon> GetIcon(string name)
{
await CreateAccessPolicyIfNotExist();
var blob = await _blobStorageClient.GetBlob($"{_appliedFolder}/{name}");
return new DeviceIcon(name, blob);
}
public async Task<DeviceIconResult> GetIcons(int skip, int take)
{
await CreateAccessPolicyIfNotExist();
string folderPrefix = _appliedFolder + "/";
var blobs = await _blobStorageClient.ListBlobs(folderPrefix, true);
var icons = blobs.Select(b =>
@ -73,21 +77,25 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Infr
public async Task<DeviceIcon> SaveIcon(string name)
{
await CreateAccessPolicyIfNotExist();
var appliedBlob = await _blobStorageClient.GetBlob($"{_appliedFolder}/{name}");
return new DeviceIcon(name, appliedBlob);
}
public async Task<bool> DeleteIcon(string name)
{
return await _blobStorageClient.DeleteBlob($"{_appliedFolder}/{name}");
return await _blobStorageClient.DeleteBlob($"{_appliedFolder}/{name}");
}
public string GetIconStorageUriPrefix()
public async Task<string> GetIconStorageUriPrefix()
{
return string.Format("{0}/{1}/", _blobStorageClient.GetContainerUri().Result, _appliedFolder);
await CreateAccessPolicyIfNotExist();
return string.Format("{0}/{1}/", _blobStorageClient.GetContainerUri().Result, _appliedFolder);
}
private void CreateSASPolicyIfNotExist()
private async Task CreateAccessPolicyIfNotExist()
{
var writePolicy = new SharedAccessBlobPolicy
{
@ -95,7 +103,7 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Infr
SharedAccessExpiryTime = DateTime.UtcNow.AddYears(10)
};
_blobStorageClient.CreateSASPolicyIfNotExist(_writePolicyName, writePolicy).Wait();
await _blobStorageClient.CreateAccessPolicyIfNotExist(BlobContainerPublicAccessType.Blob, _writePolicyName, writePolicy);
}
}
}

Просмотреть файл

@ -12,6 +12,6 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Infr
Task<DeviceIconResult> GetIcons(int skip, int take);
Task<DeviceIcon> SaveIcon(string name);
Task<bool> DeleteIcon(string name);
string GetIconStorageUriPrefix();
Task<string> GetIconStorageUriPrefix();
}
}

Просмотреть файл

@ -60,11 +60,11 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.
}
[RequirePermission(Permission.ViewDevices)]
public ActionResult Index(string filterId)
public async Task<ActionResult> Index(string filterId)
{
ViewBag.FilterId = filterId;
ViewBag.HasManageJobsPerm = PermsChecker.HasPermission(Permission.ManageJobs);
ViewBag.IconBaseUrl = _iconRepository.GetIconStorageUriPrefix();
ViewBag.IconBaseUrl = await _iconRepository.GetIconStorageUriPrefix();
ViewBag.IconTagName = Constants.DeviceIconTagName;
return View();
@ -305,7 +305,7 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.
deviceModel.IsCellular = device.SystemProperties.ICCID != null;
deviceModel.Iccid = device.SystemProperties.ICCID; // todo: try get rid of null checks
ViewBag.IconBaseUrl = _iconRepository.GetIconStorageUriPrefix();
ViewBag.IconBaseUrl = await _iconRepository.GetIconStorageUriPrefix();
return PartialView("_DeviceDetails", deviceModel);
}

Просмотреть файл

@ -48,6 +48,7 @@
this.fileChanged = function (f) {
self.file = f.files[0];
if (!f.value) return;
$('#filePathBox').val(f.value);
if (self.file && self.file.size > self.maxSizeInMB * 1024 * 1024) {
IoTApp.Helpers.Dialog.displayError(resources.overSizedFile);

Просмотреть файл

@ -20,7 +20,6 @@
var self = this;
this.file = null;
this.maxSizeInMB = 4;
this.sizeOk = false;
this.pageSize = 10;
this.apiRoute = '/api/v1/icons/';
@ -78,11 +77,13 @@
this.fileChanged = function (f) {
self.file = f.files[0];
if (!f.value) return;
$('#filePathBox').val(f.value);
if (self.file && self.file.size > self.maxSizeInMB * 1024 * 1024) {
IoTApp.Helpers.Dialog.displayError(resources.overSizedFile);
} else {
self.sizeOk = true;
self.uploadImage();
self.actionType(resources.uploadActionType);
}
};
@ -223,10 +224,6 @@
$('#chooseFileBtn').click(function () {
$("#file").click();
if (self.sizeOk) {
self.uploadImage();
}
self.actionType(resources.uploadActionType);
return false;
});

Просмотреть файл

@ -105,11 +105,11 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.UnitTests.Infras
}
[Fact]
public void GetIconStorageUrlPrefixTest()
public async void GetIconStorageUrlPrefixTest()
{
string containerUri = "https://account1.blob.core.windows.net/deviceicons";
_blobStorageClientMock.Setup(x => x.GetContainerUri()).ReturnsAsync(containerUri);
string url = deviceIconRepository.GetIconStorageUriPrefix();
string url = await deviceIconRepository.GetIconStorageUriPrefix();
Assert.True(url.StartsWith(containerUri));
}
}