forked from Emrecan16/rs-webapi2-advance-seed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from RemmSoft/feature/Core-43
#43 : File service done.
- Loading branch information
Showing
18 changed files
with
589 additions
and
13 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
using RS.Core.Const; | ||
using RS.Core.Service; | ||
using RS.Core.Service.DTOs; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
using System.Web.Hosting; | ||
using System.Web.Http; | ||
using System.Web.Http.Description; | ||
|
||
namespace RS.Core.Controllers | ||
{ | ||
[Authorize] | ||
[RoutePrefix("api/File")] | ||
public class FileController : ApiController | ||
{ | ||
IFileService fileService = null; | ||
public FileController(IFileService _fileService) | ||
{ | ||
fileService = _fileService; | ||
} | ||
|
||
[Route("Upload"), HttpPost] | ||
public async Task<IHttpActionResult> Upload(Guid refID,string screenCode) | ||
{ | ||
#region Condition | ||
|
||
/// Checks whether the parameters are null. | ||
if (refID == null || screenCode == null) | ||
return BadRequest(Messages.FUE0002); | ||
|
||
/// Controls whether the screen code is in the <see cref="ScreenCodes"/> class. | ||
if (!typeof(ScreenCodes).GetFields().Any(x => x.Name == screenCode)) | ||
return BadRequest(Messages.GNW0002); | ||
|
||
/// Checks the media type for the file-s to be uploaded. | ||
if (!Request.Content.IsMimeMultipartContent()) | ||
return Content(HttpStatusCode.UnsupportedMediaType, Messages.FUW0001); | ||
#endregion | ||
|
||
/// `localPath` and `useCloud` is get from Web.Config. | ||
string localPath = HostingEnvironment.MapPath(ConfigurationManager.AppSettings["fileServiceLocalPath"]); | ||
bool useCloud = Convert.ToBoolean(ConfigurationManager.AppSettings["useCloud"]); | ||
|
||
var provider = new MultipartFormDataStreamProvider(localPath); | ||
|
||
try | ||
{ | ||
/// Loads the files into the local storage. | ||
await Request.Content.ReadAsMultipartAsync(provider); | ||
|
||
/// Check is exist valid file. | ||
if (provider.FileData.Count == 0) | ||
return BadRequest(Messages.FUE0001); | ||
|
||
IList<FileDto> modelList = new List<FileDto>(); | ||
|
||
foreach (MultipartFileData file in provider.FileData) | ||
{ | ||
string originalName = file.Headers.ContentDisposition.FileName; | ||
if (originalName.StartsWith("\"") && originalName.EndsWith("\"")) | ||
{ | ||
originalName = originalName.Trim('"'); | ||
} | ||
if (originalName.Contains(@"/") || originalName.Contains(@"\")) | ||
{ | ||
originalName = Path.GetFileName(originalName); | ||
} | ||
|
||
FileDto fileDto = new FileDto | ||
{ | ||
RefID = refID, | ||
ScreenCode = screenCode, | ||
OriginalName = Path.GetFileNameWithoutExtension(originalName), | ||
StorageName = Path.GetFileName(file.LocalFileName), | ||
Extension = Path.GetExtension(originalName).ToLower(), | ||
Size = new FileInfo(file.LocalFileName).Length | ||
}; | ||
|
||
modelList.Add(fileDto); | ||
} | ||
|
||
if (useCloud) | ||
await fileService.SendCloud(modelList,localPath); | ||
|
||
await fileService.Add(modelList, IdentityClaimsValues.UserID<Guid>()); | ||
|
||
return Ok(Messages.Ok); | ||
} | ||
catch (Exception exMessage) | ||
{ | ||
return Content(HttpStatusCode.InternalServerError, exMessage); | ||
} | ||
} | ||
|
||
[Route("Download"), HttpGet] | ||
public async Task<IHttpActionResult> Download(Guid id) | ||
{ | ||
var model = await fileService.GetByID(id); | ||
|
||
if (model == null) | ||
return BadRequest(Messages.GNE0001); | ||
|
||
/// `localPath` is get from Web.Config. | ||
string localPath = HostingEnvironment.MapPath(ConfigurationManager.AppSettings["fileServiceLocalPath"]); | ||
|
||
string root = localPath + "\\" + model.StorageName; | ||
|
||
byte[] fileData = File.ReadAllBytes(root); | ||
var stream = new MemoryStream(fileData, 0, fileData.Length); | ||
|
||
var response = new HttpResponseMessage(HttpStatusCode.OK) | ||
{ | ||
Content = new ByteArrayContent(stream.ToArray()) | ||
}; | ||
|
||
response.Content.Headers.ContentDisposition = | ||
new ContentDispositionHeaderValue("attachment") | ||
{ | ||
FileName = model.OriginalName + "." + model.Extension, | ||
Size=model.Size | ||
}; | ||
|
||
response.Content.Headers.ContentType = | ||
new MediaTypeHeaderValue("application/octet-stream"); | ||
|
||
IHttpActionResult result = ResponseMessage(response); | ||
return result; | ||
} | ||
|
||
[Route("Get"), HttpGet] | ||
[ResponseType(typeof(IEnumerable<FileListDto>))] | ||
public async Task<IHttpActionResult> GetList(Guid refID, string screenCode) | ||
{ | ||
/// Controls whether the screen code is in the <see cref="ScreenCodes"/> class. | ||
if (!typeof(ScreenCodes).GetFields().Any(x => x.Name == screenCode)) | ||
return BadRequest(Messages.GNW0002); | ||
|
||
var result = await fileService.GetList(refID,screenCode); | ||
|
||
if (result == null || result.Count <= 0) | ||
result = new List<FileListDto>(); | ||
|
||
return Ok(result); | ||
} | ||
|
||
} | ||
} |
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
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
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
29 changes: 29 additions & 0 deletions
29
RS.Core.Data/Migrations/201710060826502_File-Migration.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace RS.Core.Data.Migrations | ||
{ | ||
using System; | ||
using System.Data.Entity.Migrations; | ||
|
||
public partial class FileMigration : DbMigration | ||
{ | ||
public override void Up() | ||
{ | ||
CreateTable( | ||
"dbo.File", | ||
c => new | ||
{ | ||
ID = c.Guid(nullable: false), | ||
RefID = c.Guid(nullable: false), | ||
ScreenCode = c.String(nullable: false, maxLength: 5), | ||
OriginalName = c.String(nullable: false, maxLength: 30), | ||
StorageName = c.String(nullable: false, maxLength: 50), | ||
Extension = c.String(nullable: false, maxLength: 10), | ||
Size = c.Long(), | ||
CreateDT = c.DateTime(nullable: false), | ||
UpdateDT = c.DateTime(), | ||
CreateBy = c.Guid(nullable: false), | ||
UpdateBy = c.Guid(), | ||
IsDeleted = c.Boolean(nullable: false), | ||
}) | ||
.PrimaryKey(t => t.ID); | ||
|
||
} | ||
|
||
public override void Down() | ||
{ | ||
DropTable("dbo.File"); | ||
} | ||
} | ||
} |
Oops, something went wrong.