public ActionResult Index() { var model = new ShareCreateModel(); var list = new List<StoredFileModel>(); using (var db = new BusinessLogic.Data.SharingDataContext()) { BusinessLogic.Data.FileSet fileSet; // delete draft file sets that don't have any files var cleanupSets = db.FileSets .Where(x => x.OwnerUsername == this.User.Identity.Name && x.IsDraft && x.StoredFiles.Count == 0) .ToList(); foreach (var set in cleanupSets) { db.FileSets.Remove(set); } // find an orphan file set that we actually uploaded files to and resume it fileSet = db.FileSets .Where(x => x.OwnerUsername == this.User.Identity.Name && x.IsDraft && x.StoredFiles.Count > 0) .Include(i => i.StoredFiles) .OrderByDescending(o => o.CreateDateTime) .FirstOrDefault(); if (fileSet == null) { model.IsNew = true; fileSet = new BusinessLogic.Data.FileSet() { Name = string.Format("File share created on {0:g}", DateTime.Now), CreateDateTime = DateTime.UtcNow, OwnerUsername = this.User.Identity.Name, ExpirationDateTime = DateTime.Now.AddDays(30), IsDraft = true }; db.FileSets.Add(fileSet); db.SaveChanges(); } else { foreach (var file in fileSet.StoredFiles) { list.Add(new StoredFileModel(){ FileID = file.FileID, Filename = file.Filename, Length = FileSize.GetSize(file.Length) }); } } model.Files = list; model.Name = fileSet.Name; model.FileSetID = fileSet.FileSetID; return View(model); } }
public ActionResult Upload(int fileSetID) { try { using (var db = new BusinessLogic.Data.SharingDataContext()) { var set = db.FileSets .Where(x => x.OwnerUsername == this.User.Identity.Name && x.FileSetID == fileSetID) .Include(i => i.StoredFiles) .SingleOrDefault(); string storagePath; var fallbackFiles = new List<StoredFileModel>(); foreach (var file in set.StoredFiles) { fallbackFiles.Add(new StoredFileModel() { Filename = System.IO.Path.GetFileName(file.Filename) }); } foreach (var fileKey in this.Request.Files.AllKeys) { storagePath = this.FileStorageService.Put(this.Request.Files[fileKey].InputStream); db.StoredFiles.Add(new BusinessLogic.Data.StoredFile() { FileSet = set, ContentType = this.Request.Files[fileKey].ContentType, Length = this.Request.Files[fileKey].ContentLength, Filename = System.IO.Path.GetFileName(this.Request.Files[fileKey].FileName), OwnerUsername = this.User.Identity.Name, StoragePath = storagePath, CreateUsername = this.User.Identity.Name, CreateDateTime = DateTime.UtcNow }); fallbackFiles.Add(new StoredFileModel() { Filename = System.IO.Path.GetFileName(this.Request.Files[fileKey].FileName) }); } db.SaveChanges(); if (Request.IsAjaxRequest() || Request.Form["IsFlashRequest"] == "1") { return new HttpStatusCodeResult(200); } else { var model = new ShareCreateModel() { FileSetID = fileSetID, Files = fallbackFiles }; return View("Index", model); } } } catch (Exception ex) { Response.Status = "500 Error"; Response.Write(ex.Message); Response.End(); return null; } }