More pagination

converter settings json
This commit is contained in:
Kilian Hofmann 2023-11-22 07:51:16 +01:00
parent f52ca435eb
commit 515cbd8a20
9 changed files with 232 additions and 153 deletions

View File

@ -1,37 +1,87 @@
using Docnet.Core.Models; using Docnet.Core;
using Docnet.Core.Exceptions;
using Docnet.Core.Models;
using Docnet.Core.Readers; using Docnet.Core.Readers;
using Docnet.Core; using khofmann;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Docnet.Core.Exceptions; using System.Text.Json;
using System.Text.Json.Serialization;
#region Defines
const double MM2IN = 25.4;
#endregion
#region Main Code #region Main Code
/*
if (args.Length == 0) if (args.Length == 0)
{ {
Console.WriteLine("No PDF specified"); Console.WriteLine("No PDF specified");
return 1; return 1;
} }
Console.WriteLine($"Conversion for {args[0]}"); Console.WriteLine($"Conversion for {args[0]}\n");
Console.WriteLine("Using configuration"); */
Console.WriteLine("\tA4 at 96dpi"); Tuple<int, int> size = ReadConfig();
Console.WriteLine(); return ReadPDF("MD11_FCOM_vol1.pdf", size.Item1, size.Item2);
return ReadPDF(args[0]);
#endregion #endregion
#region Functions #region Functions
int ReadPDF(string path) Tuple<int, int> ReadConfig()
{
try
{
using FileStream stream = new("config.json", FileMode.Open, FileAccess.Read);
Settings? settings = JsonSerializer.Deserialize<Settings>(stream);
if (settings != null)
{
Paper paper = settings.PaperSizes.First(p => p.Name.Equals(settings.SelectedPaper));
if (paper != null)
{
if (paper.Unit == Unit.Millimetre)
{
int width = Convert.ToInt32(Math.Round(paper.Width / MM2IN * settings.DPI));
int height = Convert.ToInt32(Math.Round(paper.Height / MM2IN * settings.DPI));
Console.WriteLine($"Using {paper.Name}, {width}mm x {height}mm at {settings.DPI} DPI");
if (width < height) return new(width, height);
else return new(height, width);
}
else
{
int width = Convert.ToInt32(Math.Round(paper.Width * settings.DPI));
int height = Convert.ToInt32(Math.Round(paper.Height * settings.DPI));
Console.WriteLine($"Using {paper.Name}, ${width}in x {height}in at ${settings.DPI}");
if (width < height) return new(width, height);
else return new(height, width);
}
}
}
}
catch { }
Console.WriteLine("Using default A4 at 96 dpi\n");
return new(794, 1122);
}
int ReadPDF(string path, int width, int height)
{ {
string outPath = ""; string outPath = "";
try try
{ {
outPath = Path.GetFileNameWithoutExtension(path); outPath = Path.GetFileNameWithoutExtension(path);
Directory.CreateDirectory(outPath); Directory.CreateDirectory(outPath);
} catch (Exception e) }
catch (Exception e)
{ {
Console.Error.WriteLine("Error creating file directory"); Console.Error.WriteLine("Error creating file directory");
Console.Error.WriteLine(e.Message); Console.Error.WriteLine(e.Message);
@ -41,54 +91,52 @@ int ReadPDF(string path)
try try
{ {
using (IDocReader docReader = DocLib.Instance.GetDocReader(path, new PageDimensions(794, 1122))) using IDocReader docReader = DocLib.Instance.GetDocReader(path, new PageDimensions(width, height));
int pages = docReader.GetPageCount();
Console.WriteLine($"Converting {pages} pages");
for (int i = 0; i < pages; i++)
{ {
int pages = docReader.GetPageCount(); using (IPageReader pageReader = docReader.GetPageReader(i))
Console.WriteLine($"Converting {pages} pages");
for (int i = 0; i < pages; i++)
{ {
using (IPageReader pageReader = docReader.GetPageReader(i)) byte[] rawBytes = pageReader.GetImage();
int _width = pageReader.GetPageWidth();
int _height = pageReader.GetPageHeight();
using (Bitmap doc = new(_width, _height, PixelFormat.Format32bppArgb))
using (Bitmap bmp = new(_width, _height, PixelFormat.Format32bppArgb))
{ {
byte[] rawBytes = pageReader.GetImage(); AddBytes(bmp, rawBytes);
int width = pageReader.GetPageWidth();
int height = pageReader.GetPageHeight();
using (Bitmap doc = new Bitmap(width, height, PixelFormat.Format32bppArgb)) Graphics g = Graphics.FromImage(doc);
using (Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb)) g.FillRegion(Brushes.White, new(new Rectangle(0, 0, _width, _height)));
g.DrawImage(bmp, new Point(0, 0));
g.Save();
if (i == 0)
{ {
AddBytes(bmp, rawBytes); Bitmap thumb = new(doc, new(doc.Width / 10, doc.Height / 10));
using (MemoryStream stream = new())
Graphics g = Graphics.FromImage(doc);
g.FillRegion(Brushes.White, new Region(new Rectangle(0, 0, width, height)));
g.DrawImage(bmp, new Point(0, 0));
g.Save();
if (i == 0)
{
Bitmap thumb = new Bitmap(doc, new Size(doc.Width / 10, doc.Height / 10));
using (MemoryStream stream = new MemoryStream())
{
//saving and exporting
thumb.Save(stream, ImageFormat.Jpeg);
Console.WriteLine($"Ouputing tumbnail");
File.WriteAllText($"{outPath}\\thumb.bjpg", Convert.ToBase64String(stream.ToArray()));
};
}
using (MemoryStream stream = new MemoryStream())
{ {
//saving and exporting //saving and exporting
doc.Save(stream, ImageFormat.Jpeg); thumb.Save(stream, ImageFormat.Jpeg);
Console.WriteLine($"Ouputing page {i + 1}"); Console.WriteLine($"Ouputing tumbnail");
File.WriteAllText($"{outPath}\\{i + 1}.bjpg", Convert.ToBase64String(stream.ToArray())); File.WriteAllText($"{outPath}\\thumb.bjpg", Convert.ToBase64String(stream.ToArray()));
}; };
}
using (MemoryStream stream = new())
{
//saving and exporting
doc.Save(stream, ImageFormat.Jpeg);
Console.WriteLine($"Ouputing page {i + 1}");
File.WriteAllText($"{outPath}\\{i + 1}.bjpg", Convert.ToBase64String(stream.ToArray()));
}; };
}; };
}; };
} };
} }
catch(DocnetLoadDocumentException) catch (DocnetLoadDocumentException)
{ {
Console.Error.WriteLine("File is not a PDF"); Console.Error.WriteLine("File is not a PDF");
Console.ReadKey(); Console.ReadKey();
@ -101,7 +149,7 @@ int ReadPDF(string path)
void AddBytes(Bitmap bmp, byte[] rawBytes) void AddBytes(Bitmap bmp, byte[] rawBytes)
{ {
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); Rectangle rect = new(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.WriteOnly, bmp.PixelFormat); BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.WriteOnly, bmp.PixelFormat);
nint pNative = bmpData.Scan0; nint pNative = bmpData.Scan0;
@ -110,4 +158,33 @@ void AddBytes(Bitmap bmp, byte[] rawBytes)
bmp.UnlockBits(bmpData); bmp.UnlockBits(bmpData);
} }
#endregion
#region classes
namespace khofmann
{
public class Settings
{
public Paper[] PaperSizes { get; set; } = [];
public string? SelectedPaper { get; set; }
public int DPI { get; set; }
}
public class Paper
{
public string Name { get; set; } = "";
public double Width { get; set; }
public double Height { get; set; }
[JsonConverter(typeof(JsonStringEnumConverter))]
public Unit Unit { get; set; }
}
public enum Unit
{
Inch,
Millimetre
}
}
#endregion #endregion

View File

@ -0,0 +1,24 @@
{
"PaperSizes": [
{
"Name": "A4",
"Width": 210,
"Height": 297,
"Unit": "Millimetre"
},
{
"Name": "A5",
"Width": 148.5,
"Height": 210,
"Unit": "Millimetre"
},
{
"Name": "Letter",
"Width": 8.5,
"Height": 11,
"Unit": "Inch"
}
],
"SelectedPaper": "A4",
"DPI": 96
}

View File

@ -8,13 +8,18 @@
<IsPublishable>False</IsPublishable> <IsPublishable>False</IsPublishable>
<Trimming>full</Trimming> <Trimming>full</Trimming>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PlatformTarget>x64</PlatformTarget>
<Platforms>x64</Platforms>
<BaseIntermediateOutputPath>obj\</BaseIntermediateOutputPath>
<BaseOutputPath>bin\</BaseOutputPath>
<GenerateDocumentationFile>False</GenerateDocumentationFile>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<WarningLevel>9999</WarningLevel> <WarningLevel>9999</WarningLevel>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<WarningLevel>9999</WarningLevel> <WarningLevel>9999</WarningLevel>
</PropertyGroup> </PropertyGroup>
@ -23,4 +28,10 @@
<PackageReference Include="System.Drawing.Common" Version="8.0.0" /> <PackageReference Include="System.Drawing.Common" Version="8.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="config.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>

View File

@ -3,18 +3,18 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.8.34309.116 VisualStudioVersion = 17.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "converter", "converter.csproj", "{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "converter", "converter.csproj", "{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU Release|x64 = Release|x64
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Debug|x64.ActiveCfg = Debug|x64
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Debug|Any CPU.Build.0 = Debug|Any CPU {F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Debug|x64.Build.0 = Debug|x64
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Release|Any CPU.ActiveCfg = Release|Any CPU {F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Release|x64.ActiveCfg = Release|x64
{F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Release|Any CPU.Build.0 = Release|Any CPU {F7DD439C-CE05-4E1B-AD8F-1D66BAB2C132}.Release|x64.Build.0 = Release|x64
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -3,10 +3,9 @@ import { COMMANDS, DATA, LIST, LOAD } from '../constants';
interface IDataContext { interface IDataContext {
list?: IList[]; list?: IList[];
dirCount?: number;
file?: string; file?: string;
isLoading?: boolean; isLoading?: boolean;
refresh?: (path?: string, offset?: number) => void; refresh?: (path?: string) => void;
load?: (path: string) => void; load?: (path: string) => void;
} }
@ -21,10 +20,10 @@ export interface IList {
export const DataContext = createContext<IDataContext>({}); export const DataContext = createContext<IDataContext>({});
export const useData = () => { export const useData = () => {
const { list, dirCount, file, isLoading, refresh, load } = useContext(DataContext); const { list, file, isLoading, refresh, load } = useContext(DataContext);
if (list !== undefined && dirCount !== undefined && file !== undefined && isLoading !== undefined && refresh && load) if (list !== undefined && file !== undefined && isLoading !== undefined && refresh && load)
return { list, dirCount, file, isLoading, refresh, load }; return { list, file, isLoading, refresh, load };
else throw new Error("Couldn't find context. Is your component inside a DataProvider?"); else throw new Error("Couldn't find context. Is your component inside a DataProvider?");
}; };
@ -34,7 +33,6 @@ interface IDataContextProps {
const DataContextProvider: FC<PropsWithChildren<IDataContextProps>> = ({ dataListener, children }) => { const DataContextProvider: FC<PropsWithChildren<IDataContextProps>> = ({ dataListener, children }) => {
const [list, setList] = useState<IList[]>([]); const [list, setList] = useState<IList[]>([]);
const [dirCount, setDirCount] = useState(0);
const [file, setFile] = useState(''); const [file, setFile] = useState('');
const [isLoading, setIsLoading] = useState(true); const [isLoading, setIsLoading] = useState(true);
@ -51,8 +49,7 @@ const DataContextProvider: FC<PropsWithChildren<IDataContextProps>> = ({ dataLis
const json = JSON.parse(args); const json = JSON.parse(args);
switch (json.id) { switch (json.id) {
case LIST: case LIST:
setDirCount(json.count); setList((json.data as IList[]).sort((a, b) => a.name.localeCompare(b.name)));
setList(json.data);
window.dispatchEvent(new Event('resize')); window.dispatchEvent(new Event('resize'));
break; break;
case LOAD: case LOAD:
@ -66,9 +63,9 @@ const DataContextProvider: FC<PropsWithChildren<IDataContextProps>> = ({ dataLis
setIsLoading(false); setIsLoading(false);
}, []); }, []);
const refresh = (path = '', offset = 0) => { const refresh = (path = '') => {
setIsLoading(true); setIsLoading(true);
dataListener.call('COMM_BUS_WASM_CALLBACK', COMMANDS, JSON.stringify({ cmd: LIST, path, offset })); dataListener.call('COMM_BUS_WASM_CALLBACK', COMMANDS, JSON.stringify({ cmd: LIST, path }));
}; };
const load = (path: string) => { const load = (path: string) => {
@ -76,9 +73,7 @@ const DataContextProvider: FC<PropsWithChildren<IDataContextProps>> = ({ dataLis
dataListener.call('COMM_BUS_WASM_CALLBACK', COMMANDS, JSON.stringify({ cmd: LOAD, file: path })); dataListener.call('COMM_BUS_WASM_CALLBACK', COMMANDS, JSON.stringify({ cmd: LOAD, file: path }));
}; };
return ( return <DataContext.Provider value={{ list, file, isLoading, refresh, load }}>{children}</DataContext.Provider>;
<DataContext.Provider value={{ list, dirCount, file, isLoading, refresh, load }}>{children}</DataContext.Provider>
);
}; };
export default DataContextProvider; export default DataContextProvider;

View File

@ -17,13 +17,13 @@ const getPages = (dirCount: number) => {
const ListPage: FC = () => { const ListPage: FC = () => {
const { navigate, getProps } = useRouter(); const { navigate, getProps } = useRouter();
const { list, dirCount, isLoading, refresh } = useData(); const { list, isLoading, refresh } = useData();
const { path: _path } = getProps() as { path: string }; const { path: _path } = getProps() as { path: string };
const guid = useRef(v4()); const guid = useRef(v4());
const [path, setPath] = useState(_path ?? '/'); const [path, setPath] = useState(_path ?? '/');
const [offset, setOffset] = useState([0]); const [offset, setOffset] = useState(_path ? new Array(_path.split('/').length).fill(0) : [0]);
const [pageJump, setPageJump] = useState('1'); const [pageJump, setPageJump] = useState('1');
return ( return (
@ -39,10 +39,10 @@ const ListPage: FC = () => {
let newPath = prev.split('/').slice(0, -2).join('/') + '/'; let newPath = prev.split('/').slice(0, -2).join('/') + '/';
if (newPath === '/') newPath = '/'; if (newPath === '/') newPath = '/';
setOffset((prev) => { setOffset((prev) => {
const _offset = prev.pop(); prev.pop();
refresh(newPath, _offset);
return [...prev]; return [...prev];
}); });
refresh(newPath);
return newPath; return newPath;
}); });
}} }}
@ -76,9 +76,8 @@ const ListPage: FC = () => {
try { try {
let page = Number.parseInt(pageJump); let page = Number.parseInt(pageJump);
if (isNaN(page)) page = offset.slice(-1)[0]; if (isNaN(page)) page = offset.slice(-1)[0];
page = Math.max(1, Math.min(getPages(dirCount) + 1, page)); page = Math.max(1, Math.min(getPages(list.length) + 1, page));
if ((page - 1) * MAX_LIST !== offset.slice(-1)[0]) { if ((page - 1) * MAX_LIST !== offset.slice(-1)[0]) {
refresh(path, (page - 1) * MAX_LIST);
setOffset((prev) => { setOffset((prev) => {
prev.pop(); prev.pop();
return [...prev, (page - 1) * MAX_LIST]; return [...prev, (page - 1) * MAX_LIST];
@ -100,7 +99,6 @@ const ListPage: FC = () => {
const curOffset = offset.slice(-1)[0]; const curOffset = offset.slice(-1)[0];
const newOffset = curOffset - MAX_LIST >= 0 ? curOffset - MAX_LIST : 0; const newOffset = curOffset - MAX_LIST >= 0 ? curOffset - MAX_LIST : 0;
if (newOffset !== curOffset) { if (newOffset !== curOffset) {
refresh(path, newOffset);
prev.pop(); prev.pop();
return [...prev, newOffset]; return [...prev, newOffset];
} else return prev; } else return prev;
@ -110,15 +108,14 @@ const ListPage: FC = () => {
<NavigateBeforeIcon htmlColor="white" fontSize="large" /> <NavigateBeforeIcon htmlColor="white" fontSize="large" />
</Button> </Button>
<Typography variant="h5"> <Typography variant="h5">
{offset.slice(-1)[0] / MAX_LIST + 1}/{getPages(dirCount) + 1} {offset.slice(-1)[0] / MAX_LIST + 1}/{getPages(list.length) + 1}
</Typography> </Typography>
<Button <Button
onClick={() => { onClick={() => {
setOffset((prev) => { setOffset((prev) => {
const curOffset = offset.slice(-1)[0]; const curOffset = offset.slice(-1)[0];
const newOffset = curOffset + MAX_LIST < dirCount ? curOffset + MAX_LIST : curOffset; const newOffset = curOffset + MAX_LIST < list.length ? curOffset + MAX_LIST : curOffset;
if (newOffset !== curOffset) { if (newOffset !== curOffset) {
refresh(path, newOffset);
prev.pop(); prev.pop();
return [...prev, newOffset]; return [...prev, newOffset];
} else return prev; } else return prev;
@ -146,13 +143,14 @@ const ListPage: FC = () => {
</Backdrop> </Backdrop>
) : ( ) : (
<Stack spacing={2}> <Stack spacing={2}>
{list.map((entry) => ( {list.slice(offset.slice(-1)[0], offset.slice(-1)[0] + MAX_LIST).map((entry) => (
<Grid <Grid
key={entry.name} key={entry.name}
bgcolor="primary.main" bgcolor="primary.main"
onClick={() => { onClick={() => {
if (entry.type === 'directory') { if (entry.type === 'directory') {
setPath(`${path}${entry.name}/`); setPath(`${path}${entry.name}/`);
setOffset((prev) => [...prev, 0]);
refresh(`${path}${entry.name}/`); refresh(`${path}${entry.name}/`);
} }
if (entry.type === 'file') navigate('/file', { path, entry }); if (entry.type === 'file') navigate('/file', { path, entry });

View File

@ -30,7 +30,7 @@ const PDFPage: FC = () => {
Back Back
</Typography> </Typography>
</Button> </Button>
<Typography variant="h5" marginLeft={2} component="div" sx={{ flexGrow: 1 }}> <Typography variant="h5" fontWeight="bold" component="div" sx={{ flexGrow: 1 }}>
{entry.name} {entry.name}
</Typography> </Typography>
<TextField <TextField

View File

@ -3,7 +3,6 @@
#include <MSFS\Legacy\gauges.h> #include <MSFS\Legacy\gauges.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h>
#include "rapidjson/document.h" #include "rapidjson/document.h"
@ -14,16 +13,6 @@
namespace khofmann namespace khofmann
{ {
/// <summary>
/// Exclude . and .. directory
/// </summary>
/// <param name="entry">Entry to check</param>
/// <returns>TRUE if not . or .., FALSE otherwise</returns>
static int excludeDotDirs(const struct dirent* entry)
{
return strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0;
}
/// <summary> /// <summary>
/// Read a file and return file contents /// Read a file and return file contents
/// </summary> /// </summary>
@ -42,7 +31,9 @@ namespace khofmann
fread(string, fsize, 1, file); fread(string, fsize, 1, file);
fclose(file); fclose(file);
return rapidjson::Value(string, alloc); rapidjson::Value retVal(string, alloc);
free(string);
return retVal;
} }
return rapidjson::Value("", alloc); return rapidjson::Value("", alloc);
@ -55,74 +46,60 @@ namespace khofmann
/// <param name="path">Path of directory</param> /// <param name="path">Path of directory</param>
/// <param name="files">Pointer to files value object</param> /// <param name="files">Pointer to files value object</param>
/// <param name="alloc">Allocator</param> /// <param name="alloc">Allocator</param>
/// <param name="offset">Offset into file list</param> static void enumerateDir(const char* path, rapidjson::Value *files, rapidjson::Document::AllocatorType& alloc)
/// <returns>Total numbers of directories</returns>
static int enumerateDir(const char* path, rapidjson::Value *files, rapidjson::Document::AllocatorType& alloc, int offset)
{ {
int count = 0;
int numDirs = 0;
DIR* d = opendir(path); DIR* d = opendir(path);
if (d) if (d)
{ {
struct dirent **dir; struct dirent* dir;
numDirs = scandir(path, &dir, excludeDotDirs, alphasort); while ((dir = readdir(d)) != NULL)
if (numDirs == -1)
{ {
log(stdout, "Error: %i", (void*)errno); if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) continue;
}
for (int i = 0; i < numDirs; i++)
{
if (i >= offset && i < offset + MAX_LIST)
{
std::string dirPath(path);
dirPath += dir[i]->d_name;
std::string thumb(dirPath);
thumb += "/thumb.bjpg";
FILE* check = fopen(thumb.c_str(), "r");
if (check)
{
fclose(check);
DIR* f;
struct dirent* file;
f = opendir(dirPath.c_str());
if (f)
{
log(stdout, "Found file %s\n", dir[i]->d_name);
while ((file = readdir(f)) != NULL)
{
if (file->d_type == DT_REG)
{
count++;
}
}
rapidjson::Value entry;
entry.SetObject();
entry.AddMember("name", rapidjson::Value(dir[i]->d_name, alloc).Move(), alloc);
entry.AddMember("pages", count - 1, alloc);
entry.AddMember("thumb", readFile(thumb.c_str(), alloc).Move(), alloc);
entry.AddMember("type", "file", alloc);
files->PushBack(entry.Move(), alloc);
}
closedir(f);
}
else
{
log(stdout, "Found directory %s\n", dir[i]->d_name);
std::string dirPath(path);
dirPath += dir->d_name;
std::string thumb(dirPath);
thumb += "/thumb.bjpg";
FILE* check = fopen(thumb.c_str(), "r");
if (check)
{
fclose(check);
DIR* f;
struct dirent* file;
f = opendir(dirPath.c_str());
if (f)
{
int count = 0;
log(stdout, "Found file %s\n", dir->d_name);
while ((file = readdir(f)) != NULL)
{
if (file->d_type == DT_REG && strstr(file->d_name, ".bjpg") != NULL)
{
count++;
}
}
rapidjson::Value entry; rapidjson::Value entry;
entry.SetObject(); entry.SetObject();
entry.AddMember("name", rapidjson::Value(dir[i]->d_name, alloc).Move(), alloc); entry.AddMember("name", rapidjson::Value(dir->d_name, alloc).Move(), alloc);
entry.AddMember("type", "directory", alloc); entry.AddMember("pages", count - 1, alloc);
entry.AddMember("thumb", readFile(thumb.c_str(), alloc).Move(), alloc);
entry.AddMember("type", "file", alloc);
files->PushBack(entry.Move(), alloc); files->PushBack(entry.Move(), alloc);
} }
closedir(f);
}
else if (dir->d_type == DT_DIR)
{
log(stdout, "Found directory %s\n", dir->d_name);
rapidjson::Value entry;
entry.SetObject();
entry.AddMember("name", rapidjson::Value(dir->d_name, alloc).Move(), alloc);
entry.AddMember("type", "directory", alloc);
files->PushBack(entry.Move(), alloc);
} }
free(dir[i]->d_name);
} }
free(dir);
} }
closedir(d); closedir(d);
return numDirs;
} }
} }

View File

@ -63,8 +63,6 @@ extern "C"
if (strcmp(cmd, "LIST") == 0) if (strcmp(cmd, "LIST") == 0)
{ {
int offset = inDoc["offset"].GetInt();
std::string path; std::string path;
path += PACKAGE_DIR; path += PACKAGE_DIR;
path += inDoc["path"].GetString(); path += inDoc["path"].GetString();
@ -77,8 +75,7 @@ extern "C"
rapidjson::Value files; rapidjson::Value files;
files.SetArray(); files.SetArray();
int numDirs = khofmann::enumerateDir(path.c_str(), &files, allocator, offset); khofmann::enumerateDir(path.c_str(), &files, allocator);
outDoc.AddMember("count", numDirs, allocator);
outDoc.AddMember("data", files.Move(), allocator); outDoc.AddMember("data", files.Move(), allocator);
rapidjson::StringBuffer strbuf; rapidjson::StringBuffer strbuf;