Esempio n. 1
0
        public async Task <IActionResult> DeleteFile(DeleteFileAddressModel model)
        {
            var(folders, fileName) = _folderLocator.SplitToPath(model.FolderNames);
            var folder = await _folderLocator.LocateSiteAndFolder(model.AccessToken, model.SiteName, folders);

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

            if (file == null)
            {
                return(this.Protocol(ErrorType.NotFound, "The file cannot be found. Maybe it has been deleted."));
            }
            _folderCleaner.DeleteFile(file);
            await _dbContext.SaveChangesAsync();

            return(this.Protocol(ErrorType.Success, $"Successfully deleted the file '{file.FileName}'"));
        }
Esempio n. 2
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());
            }
            if (!site.OpenToDownload)
            {
                _tokenEnsurer.Ensure(model.PBToken, "Download", model.SiteName, model.FolderNames);
            }
            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"));
                }
                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());
            }
        }