Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. using IVFileServer.Contracts;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Net.Http.Headers;
  9. using System.Security.Cryptography;
  10. using System.Web.Http;
  11.  
  12. namespace IVFileServer.Controllers
  13. {
  14. public class FilesController : ApiController
  15. {
  16. private const string FilesDirectory = @"C:\files";
  17.  
  18. /// <summary>
  19. /// Fetches a file. Should return the file only if the user doesn't already have it.
  20. /// </summary>
  21. [HttpGet]
  22. [Route("api/files/{*fileName}")]
  23. public HttpResponseMessage GetFile(string fileName /*, ... */)
  24. {
  25. // TODO: Implement
  26. throw new NotImplementedException();
  27. }
  28.  
  29. /// <summary>
  30. /// Computes a SHA1 hash for a file. Will throw if the file does not exist.
  31. /// </summary>
  32. private string ComputeFileHash(string filePath)
  33. {
  34. using (var shaManaged = new SHA1Managed())
  35. using (Stream fileStream = File.Open(filePath, FileMode.Open))
  36. using (Stream sourceStream = new CryptoStream(fileStream, shaManaged, CryptoStreamMode.Read))
  37. {
  38. // Read all the bytes and return the hash
  39. while (sourceStream.ReadByte() != -1) ;
  40. return BitConverter.ToString(shaManaged.Hash).Replace("-", string.Empty).ToLower();
  41. }
  42. }
  43.  
  44. /// <summary>
  45. /// Builds a HTTP response containing the contents of a file.
  46. /// </summary>
  47. private HttpResponseMessage CreateFileResponse(string filePath)
  48. {
  49. var file = File.Open(filePath, FileMode.Open);
  50. var content = new StreamContent(file);
  51. content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
  52. {
  53. FileName = Path.GetFileName(filePath)
  54. };
  55.  
  56. return new HttpResponseMessage
  57. {
  58. StatusCode = HttpStatusCode.OK,
  59. Content = content
  60. };
  61. }
  62.  
  63. private HttpResponseMessage CreateStatusCodeResponse(HttpStatusCode statusCode)
  64. {
  65. return new HttpResponseMessage(statusCode);
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement