Beispiel #1
0
        public ActionResult Abandon(int id)
        {
            using (var db = new BusinessLogic.Data.SharingDataContext())
            {
                BusinessLogic.Data.FileSet fileSet;

                fileSet = db.FileSets
                    .Where(x => x.OwnerUsername == this.User.Identity.Name && x.IsDraft && x.FileSetID == id)
                    .Include(i => i.StoredFiles)
                    .SingleOrDefault();

                if (fileSet != null)
                {
                    foreach (var file in fileSet.StoredFiles)
                    {
                        this.FileStorageService.Delete(file.StoragePath);
                    }
                    db.FileSets.Remove(fileSet);
                    db.SaveChanges();
                }
            }
            return RedirectToAction("Index");
        }
Beispiel #2
0
        public ActionResult Index()
        {
            //ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            using (var db = new BusinessLogic.Data.SharingDataContext())
            {
                var count = db.StoredFiles.Where(x=>x.OwnerUsername == this.User.Identity.Name).Count();

                var yesterday = DateTime.UtcNow.AddDays(-1);

                ViewBag.Message = string.Format("You have {0} stored file(s).", count);

                ViewBag.OrphanSetCount = db.FileSets.Count(x => x.SharedFileSets.Count == 0);// && x.CreateDateTime < yesterday);

                ViewBag.OrphanFileCount = db.StoredFiles.Count(x => x.FileSet.SharedFileSets.Count == 0 && x.SharedFiles.Count == 0);// && x.CreateDateTime < yesterday);

                ViewBag.ExpiredSetCount = db.SharedFileSets.Count(x => x.ExpirationDateTime < DateTime.UtcNow);

                ViewBag.ExpiredFileCount = db.SharedFiles.Count(x => x.ExpirationDateTime < DateTime.UtcNow);
            }

            return View();
        }
Beispiel #3
0
        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);
            }
        }
Beispiel #4
0
        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;
            }
        }
Beispiel #5
0
        public ActionResult Thumb(int id)
        {
            using (var db = new BusinessLogic.Data.SharingDataContext())
            {
                var file = db.StoredFiles
                    .Where(x => x.OwnerUsername == this.User.Identity.Name && x.FileID == id)
                    .SingleOrDefault();

                var thumbFileName = string.Concat(file.StoragePath, ".jpg");
                System.IO.Stream stream;

                if (!file.ContentType.StartsWith("image"))
                {
                    return File("~/images/trans.gif", "image/gif");
                }

                if (this.FileStorageService.Exists(thumbFileName))
                {
                    stream = this.FileStorageService.Get(thumbFileName);

                    return File(stream, "image/jpeg");
                }

                using (var srcStream = this.FileStorageService.Get(file.StoragePath))
                {
                    stream = BusinessLogic.Helpers.ThumbnailHelper.CreateThumbnail(srcStream);
                }

                this.FileStorageService.Put(stream, thumbFileName);
                stream.Position = 0;

                return File(stream, "image/jpeg");
            }
        }
Beispiel #6
0
        public ActionResult Links(SharePublishModel model)
        {
            if (ModelState.IsValid)
            {
                using (var db = new BusinessLogic.Data.SharingDataContext())
                {
                    var set = db.FileSets.Single(x => x.FileSetID == model.FileSetID);
                    var shareExpires = model.ExpirationDate;
                    var fileLinks = new List<object>();
                    var shareToken = new ShareToken(BusinessLogic.SecureRandom.GetRandomBytes(20), 0);
                    byte[] passwordBytes = null;

                    if (shareExpires > DateTime.UtcNow.AddDays(this.MaxShareTTL))
                    {
                        shareExpires = DateTime.UtcNow.AddDays(this.MaxShareTTL);
                    }

                    if (model.RequirePassword)
                    {
                        passwordBytes = Convert.FromBase64String(System.Web.Helpers.Crypto.HashPassword(model.Password));
                    }

                    set.Name = model.Name;
                    set.IsDraft = false;

                    var setShare = new BusinessLogic.Data.SharedFileSet()
                    {
                        ShareToken = shareToken.GetBytes(),
                        FileSet = set,
                        OwnerUsername = this.User.Identity.Name,
                        ExpirationDateTime = shareExpires,
                        Password = passwordBytes,
                        AllowUpload = model.AllowUpload,
                        CreateUsername = this.User.Identity.Name,
                        CreateDateTime = DateTime.UtcNow
                    };
                    db.SharedFileSets.Add(setShare);

                    BusinessLogic.Data.SharedFile fileShare;
                    ShareToken fileShareToken;

                    if (set.StoredFiles.Count > 1)
                    {
                        foreach (var file in set.StoredFiles)
                        {
                            fileShareToken = new ShareToken(BusinessLogic.SecureRandom.GetRandomBytes(20), file.FileID);

                            fileShare = new BusinessLogic.Data.SharedFile()
                            {
                                ShareToken = fileShareToken.GetBytes(),
                                StoredFile = file,
                                OwnerUsername = this.User.Identity.Name,
                                ExpirationDateTime = shareExpires,
                                Password = passwordBytes,
                                CreateUsername = this.User.Identity.Name,
                                CreateDateTime = DateTime.UtcNow
                            };

                            db.SharedFiles.Add(fileShare);

                            fileLinks.Add(new
                            {
                                url = this.CreateLink(fileShareToken),
                                fileName = file.Filename,
                                length = file.Length
                            });
                        }
                    }

                    db.SaveChanges();

                    return Json(new
                    {
                        success = true,
                        setLink = this.CreateLink(shareToken),
                        fileLinks = fileLinks
                    });
                }
            }

            return Json(new
            {
                success = false,
                errorMessage = "Something wasn't right about the data you provided."
            });
        }
Beispiel #7
0
        public ActionResult Index(LinkModel model)
        {
            var token = ShareToken.Parse(model.ShareToken);
            var tokenBytes = token.GetBytes();
            var files = new List<DownloadModel>();
            byte[] passwordHash;

            using (var db = new BusinessLogic.Data.SharingDataContext())
            {
                if (token.FileID > 0)
                {
                    var sharedFile = db.SharedFiles
                        .Where(x => x.ShareToken == tokenBytes
                            && x.ExpirationDateTime > DateTime.UtcNow)
                        .Include(i => i.StoredFile)
                        .SingleOrDefault();

                    model.PasswordRequired = sharedFile.Password != null;
                    passwordHash = sharedFile.Password;

                    files.Add(new DownloadModel()
                    {
                        ShareToken = model.ShareToken,
                        FileID = sharedFile.StoredFile.FileID,
                        FileName = sharedFile.StoredFile.Filename,
                        Length = FileSize.GetSize(sharedFile.StoredFile.Length)
                    });
                }
                else
                {
                    var sharedFileSet = db.SharedFileSets
                        .Where(x => x.ShareToken == tokenBytes
                            && x.ExpirationDateTime > DateTime.UtcNow)
                        .Include(i => i.FileSet.StoredFiles)
                        .SingleOrDefault();

                    model.PasswordRequired = sharedFileSet.Password != null;
                    passwordHash = sharedFileSet.Password;

                    model.AllowUpload = sharedFileSet.AllowUpload;
                    model.FileSetName = sharedFileSet.FileSet.Name;

                    foreach (var file in sharedFileSet.FileSet.StoredFiles)
                    {
                        files.Add(new DownloadModel()
                        {
                            ShareToken = model.ShareToken,
                            FileID = file.FileID,
                            FileName = file.Filename,
                            Length = FileSize.GetSize(file.Length)
                        });
                    }

                }

                if (model.PasswordRequired)
                {
                    if (!string.IsNullOrEmpty(model.Password))
                    {
                        model.PasswordIsValid = this.PasswordIsValidOrNotRequired(passwordHash, model.Password);
                        if (model.PasswordIsValid)
                        {
                            var cookie = CreateSessionCookie(passwordHash);
                            Response.SetCookie(cookie);
                        }
                    }
                    else
                    {
                        model.PasswordIsValid = this.ValidatePasswordCookie(passwordHash);
                    }
                }

                model.Password = null;
                model.Files = files;

                if (!model.PasswordIsValid && model.PasswordRequired)
                {
                    model = new LinkModel()
                    {
                        ShareToken = model.ShareToken,
                        Files = new List<DownloadModel>(),
                        PasswordRequired = true
                    };
                }
            }

            if (Request.IsAjaxRequest())
            {
                return Json(model);
            }
            else
            {
                return View(model);
            }
        }
Beispiel #8
0
        private BusinessLogic.Data.StoredFile GetStoredFile(DownloadModel model)
        {
            using (var db = new BusinessLogic.Data.SharingDataContext())
            {
                var shareToken = ShareToken.Parse(model.ShareToken);
                var shareTokenBytes = shareToken.GetBytes();
                byte[] sharePassword = null;
                BusinessLogic.Data.StoredFile storedFile;

                if (shareToken.FileID > 0)
                {
                    var sharedFile = db.SharedFiles
                        .Where(x => x.ShareToken == shareTokenBytes
                            && x.ExpirationDateTime > DateTime.UtcNow)
                        .Include(i => i.StoredFile)
                        .SingleOrDefault();

                    sharePassword = sharedFile.Password;
                    storedFile = sharedFile.StoredFile;
                }
                else
                {
                    var sharedFileSet = db.SharedFileSets
                        .Where(x => x.ShareToken == shareTokenBytes
                            && x.ExpirationDateTime > DateTime.UtcNow)
                        .Include(s => s.FileSet.StoredFiles)
                        .SingleOrDefault();

                    sharePassword = sharedFileSet.Password;
                    storedFile = sharedFileSet.FileSet.StoredFiles.SingleOrDefault(x => x.FileID == model.FileID);
                }

                if (!ValidatePasswordCookie(sharePassword))
                {
                    return null;
                }

                return storedFile;
            }
        }
Beispiel #9
0
        public ActionResult Upload(UploadModel model)
        {
            try
            {
                using (var db = new BusinessLogic.Data.SharingDataContext())
                {
                    var shareToken = ShareToken.Parse(model.ShareToken);
                    var shareTokenBytes = shareToken.GetBytes();

                    var sharedSet = db.SharedFileSets
                        .Where(x => x.ShareToken == shareTokenBytes && x.ExpirationDateTime > DateTime.UtcNow && x.AllowUpload)
                        .Include(i=>i.FileSet)
                        .SingleOrDefault();

                    if (sharedSet == null || !ValidatePasswordCookie(sharedSet.Password) || !sharedSet.AllowUpload)
                    {
                        return new HttpStatusCodeResult(403);
                    }

                    string storagePath;
                    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 = sharedSet.FileSet,
                            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
                        });
                    }

                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Response.ContentType = "text/plain";
                Response.Write(ex.Message);
                return new HttpStatusCodeResult(500);
            }

            if (Request.IsAjaxRequest() || Request.Form["IsFlashRequest"] == "1")
            {
                Response.ContentType = "text/plain";
                Response.Write("OK");
                return new HttpStatusCodeResult(200);
            }
            else
            {
                return RedirectToAction("Index", new { id = model.ShareToken });
            }
        }