Esempio n. 1
0
        public async Task <IActionResult> InSites(InSitesAddressModel model)
        {
            var site = await _dbContext
                       .Sites
                       .Include(t => t.Root)
                       .Include(t => t.Root.SubFolders)
                       .Include(t => t.Root.Files)
                       .SingleOrDefaultAsync(t => t.SiteName.ToLower() == model.SiteName);

            if (site == null)
            {
                return(NotFound());
            }
            string[] foldersWithFileName = _folderLocator.SplitStrings(model.FolderNames);
            string   fileName            = foldersWithFileName.Last();

            string[] folders = foldersWithFileName.Take(foldersWithFileName.Count() - 1).ToArray();

            var folder = await _folderLocator.LocateAsync(folders, site.Root);

            var file = folder.Files.SingleOrDefault(t => t.FileName == fileName);

            if (file == null)
            {
                return(NotFound());
            }
            string path = _configuration["StoragePath"] + $"{_}Storage{_}{file.Id}.dat";

            return(await this.AiurFile(path, file.FileName));
        }
Esempio n. 2
0
        public async Task <IActionResult> Open(OpenAddressModel model)
        {
            var site = await _dbContext
                       .Sites
                       .Include(t => t.Root)
                       .SingleOrDefaultAsync(t => t.SiteName.ToLower() == model.SiteName);

            if (site == null)
            {
                return(NotFound());
            }
            if (!site.OpenToDownload)
            {
                _tokenEnsurer.Ensure(model.PBToken, "Download", model.SiteName, model.FolderNames);
            }
            var(folders, fileName) = _folderLocator.SplitToFoldersAndFile(model.FolderNames);
            try
            {
                var folder = await _folderLocator.LocateAsync(folders, site.Root, false);

                if (folder == null)
                {
                    return(NotFound());
                }
                var file = folder.Files.SingleOrDefault(t => t.FileName == fileName);
                if (file == null)
                {
                    return(NotFound());
                }
                var path      = _storageProvider.GetFilePath(file.HardwareId);
                var extension = _storageProvider.GetExtension(file.FileName);
                if (ControllerContext.ActionDescriptor.AttributeRouteInfo.Name == "File")
                {
                    return(this.WebFile(path, "do-not-open"));
                }
                else if (file.FileName.IsStaticImage() && Image.DetectFormat(path) != null)
                {
                    return(await FileWithImageCompressor(path, extension));
                }
                else
                {
                    return(this.WebFile(path, extension));
                }
            }
            catch (AiurAPIModelException e) when(e.Code == ErrorType.NotFound)
            {
                return(NotFound());
            }
        }
Esempio n. 3
0
        public async Task <IActionResult> Open(OpenAddressModel model)
        {
            var site = await _dbContext
                       .Sites
                       .Include(t => t.Root)
                       .Include(t => t.Root.SubFolders)
                       .Include(t => t.Root.Files)
                       .SingleOrDefaultAsync(t => t.SiteName.ToLower() == model.SiteName);

            if (site == null)
            {
                return(NotFound());
            }
            var(folders, fileName) = _folderLocator.SplitToPath(model.FolderNames);
            try
            {
                var folder = await _folderLocator.LocateAsync(folders, site.Root, false);

                var file = folder.Files.SingleOrDefault(t => t.FileName == fileName);
                if (file == null)
                {
                    return(NotFound());
                }
                var path      = _configuration["StoragePath"] + $"{_}Storage{_}{file.Id}.dat";
                var extension = Path.GetExtension(file.FileName).TrimStart('.').ToLower();
                if (ControllerContext.ActionDescriptor.AttributeRouteInfo.Name == "File")
                {
                    return(this.WebFile(path, "do-not-open"));
                }
                if (file.FileName.IsStaticImage() && Image.DetectFormat(path) != null)
                {
                    return(await this.FileWithImageCompressor(path, extension));
                }
                else
                {
                    return(this.WebFile(path, extension));
                }
            }
            catch (AiurAPIModelException e) when(e.Code == ErrorType.NotFound)
            {
                return(NotFound());
            }
        }
Esempio n. 4
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."
            }));
        }