Skip to content

Commit

Permalink
Fixed conccurency issues with Amazon
Browse files Browse the repository at this point in the history
  • Loading branch information
Soren Nielsen committed Aug 23, 2012
1 parent 86a1066 commit 8960aad
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 54 deletions.
72 changes: 50 additions & 22 deletions NooSphere.Cloud.ActivityManager/Controllers/Api/FileController.cs
Expand Up @@ -46,22 +46,27 @@ public class FileController : BaseController
/// <param name="resourceId"> Guid representation of the resource Id. </param>
/// <returns> byte[] of the given resource </returns>
[RequireUser]
public HttpResponseMessage Get(Guid activityId, Guid resourceId)
public Task<HttpResponseMessage> Get(Guid activityId, Guid resourceId)
{
var response = new HttpResponseMessage();
try {
var stream = _fileStorage.Download(GenerateId(activityId, resourceId));
if (stream != null)
{
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(stream);
}
try
{
var task = _fileStorage.DownloadFileAsync(GenerateId(activityId, resourceId));
var result = task.ContinueWith(o => new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StreamContent(task.Result)
});

return result;
} catch(Exception e)
{
response.StatusCode = HttpStatusCode.InternalServerError;
response.Content = new StringContent(e.Message);
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.InternalServerError,
Content = new StringContent(e.Message)
};
throw new HttpResponseException(response);
}
return response;
}

/// <summary>
Expand All @@ -72,19 +77,42 @@ public HttpResponseMessage Get(Guid activityId, Guid resourceId)
[RequireUser]
public Task<HttpResponseMessage> Post(Guid activityId, Guid resourceId)
{
var resource = new ActivityController().GetActivity(activityId).Resources.SingleOrDefault(r => r.Id == resourceId);
if(resource != null) {
var task = Request.Content.ReadAsStreamAsync();
var result = task.ContinueWith(o =>
try {
var resource = new ActivityController().GetActivity(activityId).Resources.SingleOrDefault(r => r.Id == resourceId);
if(resource != null) {
var task = Request.Content.ReadAsStreamAsync();
var result = task.ContinueWith(o =>
{
var uploadTask = _fileStorage.Upload(GenerateId(resource), task.Result);
return uploadTask.ContinueWith(u =>
{
if(u.Result)
{
Notifier.NotifyGroup(activityId, NotificationType.FileDownload, resource);
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
}
throw new HttpResponseException(new HttpResponseMessage
{
StatusCode = HttpStatusCode.NotFound,
Content = new StringContent("The resource was not uploaded.")
});
});
});
return result.Result;
}
throw new HttpResponseException(new HttpResponseMessage
{
if (_fileStorage.Upload(GenerateId(resource), task.Result))
Notifier.NotifyGroup(activityId, NotificationType.FileDownload, resource);
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
StatusCode = HttpStatusCode.NotFound,
Content = new StringContent("The resource was not found.")
});
} catch(Exception e)
{
throw new HttpResponseException(new HttpResponseMessage
{
StatusCode = HttpStatusCode.NotFound,
Content = new StringContent(e.Message)
});

return result;
}
return null;
}

#endregion
Expand Down
79 changes: 47 additions & 32 deletions NooSphere.Cloud.Storage/Storage/FileStorage.cs
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Collections.Specialized;
using System.IO;
using System.Threading.Tasks;
using Amazon.S3;
using Amazon.S3.Model;

Expand Down Expand Up @@ -45,48 +46,62 @@ public FileStorage(string accessKey, string accessSecret)

#region Public Methods

public Stream Download(string id)
public Task<Stream> DownloadFileAsync(string id)
{
try
{
using (var client = SetupClient())
return
client.GetObject(new GetObjectRequest().WithBucketName(BucketName).WithKey(id)).ResponseStream;
try {
using (var client = SetupClient()) {
var response = Task.Factory.FromAsync<GetObjectRequest, GetObjectResponse>(
client.BeginGetObject,
client.EndGetObject,
new GetObjectRequest().WithBucketName(BucketName).WithKey(id), null);

return response.ContinueWith(r => r.Result.ResponseStream);
}
}
catch (AmazonS3Exception e)
{
throw new Exception(e.Message);
}
}

public bool Upload(string id, Stream stream)
public Task<bool> Upload(string id, Stream stream)
{
NameValueCollection metadata;
if(Exists(id))
metadata = new NameValueCollection
{
{LastWriteTimeKey, DateTime.UtcNow.ToString("u")}
};
else
metadata = new NameValueCollection
{
{CreationTimeKey, DateTime.UtcNow.ToString("u")},
{LastWriteTimeKey, DateTime.UtcNow.ToString("u")}
};
try{
NameValueCollection metadata;
if(Exists(id))
metadata = new NameValueCollection
{
{LastWriteTimeKey, DateTime.UtcNow.ToString("u")}
};
else
metadata = new NameValueCollection
{
{CreationTimeKey, DateTime.UtcNow.ToString("u")},
{LastWriteTimeKey, DateTime.UtcNow.ToString("u")}
};

var req = new PutObjectRequest
{
BucketName = BucketName,
Key = id,
InputStream = stream,
Timeout = -1,
ReadWriteTimeout = 300000
};

var req = new PutObjectRequest
{
BucketName = BucketName,
Key = id,
InputStream = stream,
Timeout = -1,
ReadWriteTimeout = 300000
};

using (var client = SetupClient())
client.PutObject(req.WithMetaData(metadata));
using (var client = SetupClient())
{
var response = Task.Factory.FromAsync<PutObjectRequest, PutObjectResponse>(
client.BeginPutObject,
client.EndPutObject,
req.WithMetaData(metadata), null);

return true;
return response.ContinueWith(r => true);
}
} catch(AmazonS3Exception e)
{
throw new Exception(e.Message);
}
}

public DateTime LastWriteTime(string id)
Expand All @@ -102,9 +117,9 @@ public DateTime LastWriteTime(string id)
}
catch
{
return DateTime.MinValue;
}
}
return DateTime.MinValue;
}

#endregion
Expand Down

0 comments on commit 8960aad

Please sign in to comment.