Exemple #1
0
        public async Task <IActionResult> UploadFile(UploadFileAddressModel model)
        {
            var site = await _siteRepo.GetSiteByName(model.SiteName);

            if (site == null)
            {
                return(this.Protocol(ErrorType.NotFound, $"Can't find a site with name: '{model.SiteName}'!"));
            }
            if (!site.OpenToUpload)
            {
                _tokenEnsurer.Ensure(model.Token, "Upload", model.SiteName, model.FolderNames);
            }
            var folders    = _folderSplitter.SplitToFolders(model.FolderNames);
            var rootFolder = await _folderRepo.GetFolderFromId(site.RootFolderId);

            var folder = await _folderRepo.GetFolderFromPath(folders, rootFolder, model.RecursiveCreate);

            if (folder == null)
            {
                return(this.Protocol(ErrorType.NotFound, "Can't find your folder!"));
            }
            // Executing here will let the browser upload the file.
            try
            {
                var _ = HttpContext.Request.Form.Files.FirstOrDefault()?.ContentType;
            }
            catch (InvalidOperationException e)
            {
                return(this.Protocol(ErrorType.InvalidInput, e.Message));
            }
            if (HttpContext.Request.Form.Files.Count < 1)
            {
                return(this.Protocol(ErrorType.InvalidInput, "Please provide a file!"));
            }
            var file = HttpContext.Request.Form.Files.First();

            if (!new ValidFolderName().IsValid(file.FileName))
            {
                return(this.Protocol(ErrorType.InvalidInput, $"Invalid file name: '{file.FileName}'!"));
            }
            var fileName          = _folderSplitter.GetValidFileName(folder.Files.Select(t => t.FileName), file.FileName);
            var newFileHardwareId = await _fileRepo.SaveFileToDb(fileName, folder.Id, file.Length);

            await _storageProvider.Save(newFileHardwareId, file);

            var filePath = _probeLocator.GetProbeFullPath(model.SiteName, string.Join('/', folders), fileName);

            return(Ok(new UploadFileViewModel
            {
                InternetPath = _probeLocator.GetProbeOpenAddress(filePath),
                DownloadPath = _probeLocator.GetProbeDownloadAddress(filePath),
                SiteName = model.SiteName,
                FilePath = filePath,
                FileSize = file.Length,
                Code = ErrorType.Success,
                Message = "Successfully uploaded your file."
            }));
        }
Exemple #2
0
        public async Task <IActionResult> UploadFile(UploadFileAddressModel model)
        {
            var site = await _dbContext
                       .Sites
                       .Include(t => t.Root)
                       .SingleOrDefaultAsync(t => t.SiteName.ToLower() == model.SiteName.ToLower());

            if (site == null)
            {
                return(this.Protocol(ErrorType.NotFound, $"Can't find a site with name: '{model.SiteName}'!"));
            }
            if (!site.OpenToUpload)
            {
                _tokenEnsurer.Ensure(model.Token, "Upload", model.SiteName, model.FolderNames);
            }
            var folders = _folderLocator.SplitToFolders(model.FolderNames);
            var folder  = await _folderLocator.LocateAsync(folders, site.Root, model.RecursiveCreate);

            if (folder == null)
            {
                return(this.Protocol(ErrorType.NotFound, $"Can't locate your folder!"));
            }
            try
            {
                var _ = HttpContext.Request.Form.Files.FirstOrDefault()?.ContentType;
            }
            catch (InvalidOperationException e)
            {
                return(this.Protocol(ErrorType.InvalidInput, e.Message));
            }
            // Executing here will let the browser upload the file.
            if (HttpContext.Request.Form.Files.Count < 1)
            {
                return(this.Protocol(ErrorType.InvalidInput, "Please provide a file!"));
            }
            var file = HttpContext.Request.Form.Files.First();

            if (!new ValidFolderName().IsValid(file.FileName))
            {
                return(this.Protocol(ErrorType.InvalidInput, $"Invalid file name: '{file.FileName}'!"));
            }
            var newFile = new File
            {
                FileName  = _folderLocator.GetValidFileName(folder.Files.Select(t => t.FileName), file.FileName), //file.FileName,
                ContextId = folder.Id,
                FileSize  = file.Length
            };
            // Save to disk
            await _storageProvider.Save(newFile.HardwareId, file);

            // Save to database
            _dbContext.Files.Add(newFile);
            await _dbContext.SaveChangesAsync();

            var filePath = _probeLocator.GetProbeFullPath(model.SiteName, string.Join('/', folders), newFile.FileName);

            return(Json(new UploadFileViewModel
            {
                InternetPath = _probeLocator.GetProbeOpenAddress(filePath),
                DownloadPath = _probeLocator.GetProbeDownloadAddress(filePath),
                SiteName = model.SiteName,
                FilePath = filePath,
                FileSize = file.Length,
                Code = ErrorType.Success,
                Message = "Successfully uploaded your file."
            }));
        }