Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using IVFileServer.Contracts;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Security.Cryptography;
- using System.Web.Http;
- namespace IVFileServer.Controllers
- {
- public class FilesController : ApiController
- {
- private const string FilesDirectory = @"C:\files";
- /// <summary>
- /// Fetches a file. Should return the file only if the user doesn't already have it.
- /// </summary>
- [HttpGet]
- [Route("api/files/{*fileName}")]
- public HttpResponseMessage GetFile(string fileName /*, ... */)
- {
- // TODO: Implement
- throw new NotImplementedException();
- }
- /// <summary>
- /// Computes a SHA1 hash for a file. Will throw if the file does not exist.
- /// </summary>
- private string ComputeFileHash(string filePath)
- {
- using (var shaManaged = new SHA1Managed())
- using (Stream fileStream = File.Open(filePath, FileMode.Open))
- using (Stream sourceStream = new CryptoStream(fileStream, shaManaged, CryptoStreamMode.Read))
- {
- // Read all the bytes and return the hash
- while (sourceStream.ReadByte() != -1) ;
- return BitConverter.ToString(shaManaged.Hash).Replace("-", string.Empty).ToLower();
- }
- }
- /// <summary>
- /// Builds a HTTP response containing the contents of a file.
- /// </summary>
- private HttpResponseMessage CreateFileResponse(string filePath)
- {
- var file = File.Open(filePath, FileMode.Open);
- var content = new StreamContent(file);
- content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
- {
- FileName = Path.GetFileName(filePath)
- };
- return new HttpResponseMessage
- {
- StatusCode = HttpStatusCode.OK,
- Content = content
- };
- }
- private HttpResponseMessage CreateStatusCodeResponse(HttpStatusCode statusCode)
- {
- return new HttpResponseMessage(statusCode);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement