file download
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
using API.Database;
|
using API.Database;
|
||||||
using API.Services;
|
using API.Services;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
@@ -9,6 +12,7 @@ namespace API.Controllers {
|
|||||||
public class FilesController : ApiController {
|
public class FilesController : ApiController {
|
||||||
private readonly DataContext dataContext;
|
private readonly DataContext dataContext;
|
||||||
private readonly IFileService fileService;
|
private readonly IFileService fileService;
|
||||||
|
private readonly string dataPath;
|
||||||
|
|
||||||
public FilesController(
|
public FilesController(
|
||||||
DataContext dataContext,
|
DataContext dataContext,
|
||||||
@@ -16,29 +20,44 @@ namespace API.Controllers {
|
|||||||
) {
|
) {
|
||||||
this.dataContext = dataContext;
|
this.dataContext = dataContext;
|
||||||
this.fileService = fileService;
|
this.fileService = fileService;
|
||||||
|
|
||||||
|
dataPath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data");
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: api/Files/5
|
// GET: api/Files/5
|
||||||
[Route("api/songs/{songId}/files/{fileId}")]
|
[Route("api/songs/{songId}/files/{fileId}")]
|
||||||
public string Get(int id) {
|
public HttpResponseMessage Get(long songId, long fileId) {
|
||||||
return "value";
|
var response = new HttpResponseMessage(HttpStatusCode.OK);
|
||||||
|
|
||||||
|
var file = dataContext.Files.Find(fileId);
|
||||||
|
if (file == null || file.Song.ID != songId) return new HttpResponseMessage(HttpStatusCode.NotFound);
|
||||||
|
var reference = file.ReferenceFile;
|
||||||
|
var filename = file.Name;
|
||||||
|
|
||||||
|
var stream = fileService.Load(dataPath, reference.ToString());
|
||||||
|
response.Content = new StreamContent(stream);
|
||||||
|
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
|
||||||
|
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
|
||||||
|
FileName = filename
|
||||||
|
};
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST: api/Files
|
// POST: api/Files
|
||||||
[Route("api/songs/{songId}/files")]
|
[Route("api/songs/{songId}/files")]
|
||||||
public async Task<IHttpActionResult> Post(long songId, CancellationToken c) {
|
public async Task<HttpResponseMessage> Post(long songId, CancellationToken c) {
|
||||||
// get file from network input
|
// get file from network input
|
||||||
var files = HttpContext.Current.Request.Files;
|
var files = HttpContext.Current.Request.Files;
|
||||||
if (files.Count != 1) return BadRequest();
|
if (files.Count != 1) return new HttpResponseMessage(HttpStatusCode.BadRequest);
|
||||||
var file = HttpContext.Current.Request.Files[0];
|
var file = HttpContext.Current.Request.Files[0];
|
||||||
|
|
||||||
// find song in database
|
// find song in database
|
||||||
var song = dataContext.Songs.Find(songId);
|
var song = dataContext.Songs.Find(songId);
|
||||||
if (song == null) return NotFound();
|
if (song == null) return new HttpResponseMessage(HttpStatusCode.NotFound);
|
||||||
|
|
||||||
// save file in App_Data
|
// save file in App_Data
|
||||||
var path = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data");
|
var stream = file.InputStream;
|
||||||
var filename = await fileService.Save(file, path, c);
|
var filename = await fileService.Save(stream, dataPath, c);
|
||||||
|
|
||||||
// attach file reference to song
|
// attach file reference to song
|
||||||
song.Files.Add(new Models.File {
|
song.Files.Add(new Models.File {
|
||||||
@@ -47,7 +66,7 @@ namespace API.Controllers {
|
|||||||
});
|
});
|
||||||
await dataContext.SaveChangesAsync(c);
|
await dataContext.SaveChangesAsync(c);
|
||||||
|
|
||||||
return Ok();
|
return new HttpResponseMessage(HttpStatusCode.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,25 +2,31 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Web;
|
|
||||||
|
|
||||||
namespace API.Services {
|
namespace API.Services {
|
||||||
public interface IFileService {
|
public interface IFileService {
|
||||||
Task<Guid> Save(HttpPostedFile file, string path, CancellationToken c);
|
Task<Guid> Save(Stream file, string path, CancellationToken c);
|
||||||
|
Stream Load(string path, string filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class FileService : IFileService {
|
public class FileService : IFileService {
|
||||||
private const int bufferSize = 4096;
|
private const int bufferSize = 4096;
|
||||||
|
|
||||||
public async Task<Guid> Save(HttpPostedFile file, string path, CancellationToken c) {
|
public async Task<Guid> Save(Stream inputStream, string path, CancellationToken c) {
|
||||||
var filename = Guid.NewGuid();
|
var filename = Guid.NewGuid();
|
||||||
var fullPath = Path.Combine(path, filename.ToString());
|
var fullPath = Path.Combine(path, filename.ToString());
|
||||||
using (var fs = new FileStream(fullPath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize, true)) {
|
using (var fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize, true)) {
|
||||||
await file.InputStream.CopyToAsync(fs, bufferSize, c);
|
await inputStream.CopyToAsync(fileStream, bufferSize, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Stream Load(string path, string filename) {
|
||||||
|
var fullPath = Path.Combine(path, filename.ToString());
|
||||||
|
var fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize, true);
|
||||||
|
return fileStream;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,5 +8,7 @@ namespace API.Models {
|
|||||||
public Guid ReferenceFile { get; set; }
|
public Guid ReferenceFile { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public FileType FileType { get; set; }
|
public FileType FileType { get; set; }
|
||||||
|
|
||||||
|
public virtual Song Song { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user