Last active
October 15, 2015 06:27
-
-
Save gpapadopoulos/f5de004638a64970930b to your computer and use it in GitHub Desktop.
ApplicationController.UploadFile() - WebAPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost] | |
public async Task<IHttpActionResult> UploadFile() | |
{ | |
try | |
{ | |
if (!Request.Content.IsMimeMultipartContent()) | |
Request.CreateResponse(HttpStatusCode.UnsupportedMediaType); | |
#region PREVIOUS CODE - UPLOADS THE FILES TO A SERVER-SIDE DIRECTORY | |
//// TODO: *** THIS SHOULD BE A TEMP PATH ON THE SERVER-HIDDEN FROM EVERYBODY *** WE DELETE THE FILES HERE AFTER WE ARE DONE UPLOADING STUFF ON ATTUNITY RMFT*** | |
//string basePath = "C:/SERVER_FILES"; | |
//var fileRequestReader = new FileRequestReader(basePath); | |
//var fileRequest = await fileRequestReader.ReadPostAsync(Request); | |
//if (await DLSFileUtils.ExistsAsync(fileRequest.TemporaryFile.Item1) && | |
// await DLSFileUtils.ExistsAsync(Path.Combine(basePath, fileRequest.Id.ToString(), fileRequest.TemporaryFile.Item2)) == false) | |
//{ | |
// using (Stream readStream = await DLSFileUtils.OpenReadAsync(fileRequest.TemporaryFile.Item1)) | |
// using ( | |
// Stream writeStream = | |
// await | |
// DLSFileUtils.OpenWriteAsync( | |
// Path.Combine(basePath, fileRequest.Id.ToString(), fileRequest.TemporaryFile.Item2).Replace("\\", "/"))) | |
// { | |
// await readStream.CopyToAsync(writeStream); | |
// } | |
// await DLSFileUtils.DeleteAsync(fileRequest.TemporaryFile.Item1); | |
// // TODO: THIS CODE SHOULD BE OUTSIDE THE IF | |
// var fileRecord = await new FilePersistanceRepository().SaveAsync(new ApplicationAttachment | |
// { | |
// PrApplicationId = fileRequest.Id, | |
// PrFileURL = Path.Combine(basePath, fileRequest.Id.ToString(), fileRequest.TemporaryFile.Item2).Replace("\\", "/"), | |
// PrFileName = fileRequest.TemporaryFile.Item2, | |
// PrUploadDate = DateTime.Now | |
// }); | |
// return Ok(fileRecord); | |
//} | |
#endregion | |
#region GEORGEP CODE | |
/* | |
var provider = new MultipartMemoryStreamProvider(); | |
await Request.Content.ReadAsMultipartAsync(provider); | |
// DONE: Get ApplicationId from Request object | |
string applicationId = string.Empty; | |
var httpContentApplicationId = | |
provider.Contents.Where(item => item.Headers.ContentDisposition.Name.Replace("\"", string.Empty) == "Id") | |
.Select(x => x) | |
.FirstOrDefault(); | |
if (httpContentApplicationId != null) | |
{ | |
applicationId = await httpContentApplicationId.ReadAsStringAsync(); | |
} | |
// DONE: Get the file's contents from Request object | |
string fileName = string.Empty; | |
long fileSize = 0; | |
var httpContentFile = | |
provider.Contents.Where(item => item.Headers.ContentDisposition.FileName != null).Select(x => x).FirstOrDefault(); | |
Stream fileStream = null; | |
if (httpContentFile != null) | |
{ | |
fileName = applicationId + "-" + httpContentFile.Headers.ContentDisposition.FileName.Replace("\"", string.Empty); | |
fileStream = await httpContentFile.ReadAsStreamAsync(); | |
fileSize = fileStream.Length; | |
fileStream.Close(); // TODO: MAY NEED TO POSTPONE CLOSING THE Stream... | |
} | |
// TODO: NEED TO ENSURE THAT THE RmftTransfers OBJECT IS CREATED ONCE - SO WE USE THE "HttpContext.Current.Session" FOR THIS. | |
if (HttpContext.Current.Session["PrRmftTransfers"] == null) | |
{ | |
// DONE: CONSTRUCT AN RmftTransfers OBJECT IN ORDER TO BE ABLE TO CALL INTO ATTUNITY SERVER FUNCTIONALITY | |
// DONE: Need to be able to pass into the constructor the list of files selected & their sizes for each Package-Application; for Package-creation purposes | |
var fileNamesAndSizes = new Dictionary<string, long> | |
{ | |
{fileName, fileSize} | |
}; | |
HttpContext.Current.Session["PrRmftTransfers"] = new RmftTransfers(fileNamesAndSizes); | |
} | |
// At this point the object should exist in the Session object, so get it into our local object... | |
var rmftTransfers = (RmftTransfers) HttpContext.Current.Session["PrRmftTransfers"]; | |
// DONE: GET CURRENTLY LOGGED IN USER'S PROFILE-NUMBER FROM CURENTLY LOGGED-IN USER | |
string profileNumber = AuthenticationManager.CurrentUser.PrProfileNumber; | |
// DONE: IF USER DOESN'T EXIST CREATE HIM/HER | |
rmftTransfers.CreateUserIfDoesNotExistLogic(profileNumber); | |
// DONE: LOGIN TO ATTUNITY - NEED TO ENSURE THAT THE USER IS LOGGED INTO ATTUNITY ONCE | |
var userLoginResponse = rmftTransfers.UserLoginOrdinary(profileNumber); | |
// TODO: CHECK IF A PACKAGE IS NOT CREATED => CREATE ONE... | |
string packageId; | |
if (HttpContext.Current.Session["PrPackageId"] == null) | |
{ | |
// DONE: CREATE A PACKAGE ON ATTUNITY, IF NOT ALREADY CREATED... | |
HttpContext.Current.Session["PrPackageId"] = rmftTransfers.CreatePackage(profileNumber, userLoginResponse.session, applicationId); | |
packageId = (string) HttpContext.Current.Session["PrPackageId"]; | |
} | |
else // PACKAGE ALREADY CREATED... | |
{ | |
// TODO: CHECK IF THE **SAME** PACKAGE IS ALREADY CREATED... | |
packageId = (string) HttpContext.Current.Session["PrPackageId"]; | |
bool packageExists = rmftTransfers.CheckIfPackageExists(packageId, userLoginResponse.session); | |
// TODO: IF PACKAGE DOES NOT EXIST => CREATE IT... | |
if (!packageExists) | |
{ | |
HttpContext.Current.Session["PrPackageId"] = rmftTransfers.CreatePackage(profileNumber, userLoginResponse.session, | |
applicationId); | |
packageId = (string) HttpContext.Current.Session["PrPackageId"]; | |
} | |
} | |
// DONE: CREATE A SESSION-BASED DATATABLE(PACKAGEID, FILENAME) IF IT DOES NOT ALREADY EXIST... | |
if (HttpContext.Current.Session["PackageFileNameTableLookup"] == null) | |
{ | |
HttpContext.Current.Session["PackageFileNameTableLookup"] = new PackageFileNameTableLookup(); | |
} | |
// At this point the object should exist in the Session object, so get it into our local object... | |
var packageFileNameTableLookup = (PackageFileNameTableLookup)HttpContext.Current.Session["PackageFileNameTableLookup"]; | |
// TODO: CHECK IF THE FILE WITHIN THE GIVEN **PACKAGE** ALREADY EXISTS... | |
bool filenameExistsInPackage = packageFileNameTableLookup.CheckIfFileExistsInPackage(packageId, fileName); | |
// TODO: IF FILE DOES NOT ALREADY EXIST IN PACKAGE... | |
if (!filenameExistsInPackage) | |
{ | |
// DONE: UPLOAD FILES TO ATTUNITY - USE STREAM FROM "Request.Content.ReadAsStreamAsync()" AS YOUR INPUT STREAM | |
rmftTransfers.UploadChunked(packageId, fileStream, fileName, userLoginResponse.session); | |
// DONE: THE PACKAGE IS NOT CONSIDERED AS CREATED UNLESS YOU SUBMIT IT - AFTER YOU ATTACH/UPLOAD AN FILES TO IT | |
rmftTransfers.SubmitPackage(packageId, userLoginResponse.session); | |
// TODO: ADD A (PACKAGEID, FILENAME) ROW TO THE SESSION-BASED DATATABLE, IF NOT ALREADY ADDED | |
packageFileNameTableLookup.AddRow(packageId, fileName); | |
} | |
// DONE: LOGOUT SESSION FROM ATTUNITY | |
rmftTransfers.LogoutRmftUser(userLoginResponse.session); | |
// TODO: SAVES THE FILE INFO INTO THE "ApplicationAttachment" table AND RETURNS "fileRecord" TO CALLER(ANGULARJS), FOR DISPLAYING IN THE "attachments.html" TEMPLATE. | |
var fileRecord = await new FilePersistanceRepository().SaveAsync(new ApplicationAttachment | |
{ | |
PrApplicationId = Convert.ToInt64(applicationId), | |
PrFileURL = packageId, // Give PackageID in place of a FileURL | |
PrFileName = fileName, | |
PrUploadDate = DateTime.Now | |
}); | |
*/ | |
#endregion | |
return Ok(fileRecord); | |
} | |
catch (Exception ex) | |
{ | |
// TODO: LOG THE EXCEPTION SOMEWHERE | |
Log.LogMessage(ex.ToString()); | |
return InternalServerError(ex); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment