FileController.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.StaticFiles;
  7. using Procurement.Enums;
  8. using Volo.Abp.AspNetCore.Mvc;
  9. using Procurement.ViewModel;
  10. using Procurement.Helpers;
  11. using Microsoft.AspNetCore.Hosting;
  12. using System.Runtime.InteropServices;
  13. namespace Procurement.Controllers
  14. {
  15. [Produces("application/json")]
  16. [Route("api/[controller]")]
  17. public class FileController : AbpController
  18. {
  19. private readonly IWebHostEnvironment _hostEnvironment;
  20. public FileController(IWebHostEnvironment hostEnvironment)
  21. {
  22. _hostEnvironment = hostEnvironment;
  23. }
  24. [Route("version")]
  25. [HttpGet]
  26. public async Task<IActionResult> Get(string app = "sfm")
  27. {
  28. ResultCode code = ResultCode.Fail, subCode = ResultCode.Fail;
  29. string subMsg = "";
  30. string contentRootPath = _hostEnvironment.ContentRootPath;
  31. string file = "version.txt";
  32. string pathSymbol = "\\";
  33. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  34. {
  35. pathSymbol = "//";
  36. }
  37. var filePath = contentRootPath + pathSymbol + "release" + pathSymbol + app + pathSymbol + file;
  38. string version = "1.0.0";
  39. try
  40. {
  41. if (System.IO.File.Exists(filePath))
  42. {
  43. version = await System.IO.File.ReadAllTextAsync(filePath);
  44. code = ResultCode.Success;
  45. }
  46. }
  47. catch (Exception ex)
  48. {
  49. }
  50. ResultViewModel result = ResultHelper.CreateResult(code, version, subCode, subMsg);
  51. return Ok(result);
  52. }
  53. [Route("download")]
  54. [HttpGet]
  55. public FileResult downloadApk(string app = "sfm")
  56. {
  57. string contentRootPath = _hostEnvironment.ContentRootPath;
  58. string pathSymbol = "\\";
  59. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  60. {
  61. pathSymbol = "//";
  62. }
  63. string file = "app-" + app + "-release.apk";
  64. var addrUrl = contentRootPath + pathSymbol + "release" + pathSymbol + app + pathSymbol + file;
  65. var stream = System.IO.File.OpenRead(addrUrl);
  66. string fileExt = Path.GetExtension(file);
  67. //»ñÈ¡ÎļþµÄContentType
  68. var provider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
  69. {
  70. { ".apk","application/vnd.android.package-archive"},
  71. });
  72. var mime = provider.Mappings[fileExt];
  73. return File(stream, mime, Path.GetFileName(addrUrl));
  74. }
  75. }
  76. }