public async Task <IHttpActionResult> PostAsync(MimeTypeId mimeId) { if (!Request.Content.IsMimeMultipartContent()) { return(BadRequest(Resources.UploadingNotSupported)); } var context = HttpContext.Current; #region Checking the upload limit var sessionId = context.GetSessionId(); var uploadCount = await _resumeManagerRepository.GetUploadedCountAsync(sessionId, mimeId).ConfigureAwait(false); if (uploadCount >= mimeId.GetUploadLimit()) { return(BadRequest(string.Format(Resources.ExceededUploadedLimit, mimeId))); } #endregion var provider = new MultipartMemoryStreamProvider(); await Request.Content.ReadAsMultipartAsync(provider).ConfigureAwait(false); foreach (var content in provider.Contents) { #region Checking media types var headers = content.Headers; var mediaType = headers.ContentType.MediaType; var contentLength = headers.ContentLength; var availableMimeTypes = mimeId.GetMimeTypes(); if (availableMimeTypes.All(availableMimeType => availableMimeType != mediaType)) { return(BadRequest(string.Format(Resources.WrongMimeType, mediaType))); } #endregion var fileName = headers.ContentDisposition.FileName.Trim('\"'); var fileBytes = await content.ReadAsByteArrayAsync().ConfigureAwait(false); var newResume = new SpontaneousResume { SessionId = sessionId, File = fileBytes, FileName = fileName, FileType = mimeId.ToString(), FileSize = contentLength ?? default(decimal) }; if (!await _resumeManagerRepository.UploadFileAsync(newResume).ConfigureAwait(false)) { return(BadRequest(string.Format(Resources.FileExists, fileName))); } } return(Ok(Resources.FileUploaded)); }
/* * /// <summary> * /// Getting wild card mask * /// </summary> * /// <param name="mimeTypeId">Mime type Id</param> * /// <returns>Wild card mask</returns> * public static string GetWildCardMask(this MimeTypeId mimeTypeId) * { * return mimeTypeId.GetType() * .GetField(mimeTypeId.ToString()) * .GetCustomAttributes(typeof(MediaTypeAttribute), false) * .Cast<MediaTypeAttribute>() * .First() * .Wildcard; * } */ /// <summary> /// Getting all mime type strings /// </summary> /// <param name="mimeTypeId">Mime type Id</param> /// <returns>Mime type</returns> public static string[] GetMimeTypes(this MimeTypeId mimeTypeId) { return(mimeTypeId.GetType() .GetField(mimeTypeId.ToString()) .GetCustomAttributes(typeof(MediaTypeAttribute), false) .Cast <MediaTypeAttribute>() .First() .MediaTypes); }
/// <summary> /// Getting all files /// </summary> /// <param name="aSessionId">Session identifier</param> /// <param name="aMimeTypeId">Mime type identifier</param> /// <returns>All files</returns> public async Task <IEnumerable <UploadedFileInfo> > GetFilesAsync(string aSessionId, MimeTypeId aMimeTypeId) { var resumes = _hrEntities.SpontaneousResumes.Where( resume => resume.SessionId == aSessionId && resume.FileType == aMimeTypeId.ToString()); var uploads = new List <UploadedFileInfo>(); await resumes.ForEachAsync(resume => { uploads.Add(new UploadedFileInfo { Name = resume.FileName, Size = resume.FileSize, Content = resume.File, MimeType = resume.FileType }); }).ConfigureAwait(false); return(uploads); }
/// <summary> /// Get uploaded count /// </summary> /// <param name="aSessionId">Session identifier</param> /// <param name="aMimeTypeId">Mime type identifier</param> /// <returns>Uploaded count</returns> public async Task <int> GetUploadedCountAsync(string aSessionId, MimeTypeId aMimeTypeId) { return(await _hrEntities.SpontaneousResumes.CountAsync( resume => resume.SessionId == aSessionId && resume.FileType == aMimeTypeId.ToString()) .ConfigureAwait(false)); }