Skip to content

Commit

Permalink
Handle long page names with attachments (#1616)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn authored Oct 18, 2024
1 parent 0d1fb30 commit d050eb0
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 242 deletions.
25 changes: 14 additions & 11 deletions OneMore/Commands/File/ArchiveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ private void ReportResult(object sender, EventArgs e)

if (exception == null)
{
ShowMessage(string.Format(Resx.ArchiveCommand_archived, pageCount, zipPath));
ShowMessage(string.Format(
Resx.ArchiveCommand_archived, pageCount, totalCount, zipPath));
}
else
{
Expand Down Expand Up @@ -278,17 +279,19 @@ private async Task ArchivePage(XElement element, Page page, string path)
}
}

var filename = string.IsNullOrEmpty(path)
? Path.Combine(tempdir, $"{name}.htm")
: Path.Combine(tempdir, Path.Combine(path, $"{name}.htm"));
var tpath = string.IsNullOrEmpty(path) ? tempdir : Path.Combine(tempdir, path);
var filename = PathHelper.GetUniqueQualifiedFileName(tpath, ref name, ".htm");

filename = PathHelper.FitMaxPath(filename);

filename = await archivist.ExportHTML(page, filename, path, bookScope);

await ArchiveAssets(Path.GetDirectoryName(filename), path);

pageCount++;
if (filename is not null)
{
filename = await archivist.ExportHTML(page, filename, path, bookScope);
await ArchiveAssets(Path.GetDirectoryName(filename), path);
pageCount++;
}
else
{
logger.WriteLine($"archive path too long [{tpath}\\{name}.htm]");
}
}


Expand Down
8 changes: 1 addition & 7 deletions OneMore/Commands/File/Archivist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public async Task<bool> Export(string pageId, string filename,
///
/// </summary>
/// <param name="page"></param>
/// <param name="filename"></param>
/// <param name="filename">Unique qualified name</param>
/// <param name="hpath"></param>
/// <param name="bookScope"></param>
public async Task<string> ExportHTML(
Expand All @@ -131,12 +131,6 @@ public async Task<string> ExportHTML(
var path = Path.Combine(Path.GetDirectoryName(filename), name); // "c:\folder\name"
filename = Path.Combine(path, fame); // "c:\folder\name\name.htm"

if (filename.Length > PathHelper.MAX_PATH)
{
filename = PathHelper.FitMaxPath(filename);
path = Path.GetDirectoryName(filename);
}

if (PathHelper.EnsurePathExists(path))
{
if (await Export(page.PageId, filename, OneNote.ExportFormat.HTML))
Expand Down
73 changes: 37 additions & 36 deletions OneMore/Commands/File/ExportCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace River.OneMoreAddIn.Commands
{
using River.OneMoreAddIn.Settings;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand Down Expand Up @@ -93,6 +92,8 @@ private async Task Export(List<string> pageIDs)

// export...

var savedCount = 0;

using (var progress = new UI.ProgressDialog())
{
progress.SetMaximum(pageIDs.Count);
Expand All @@ -115,67 +116,67 @@ private async Task Export(List<string> pageIDs)

var title = page.Title.Trim();

if (useUnderscores)
if (title.Length == 0)
{
title = PathHelper.CleanFileName(title).Replace(' ', '_');
var pageinfo = await one.GetPageInfo(pageID);
var sectinfo = await one.GetSectionInfo(pageinfo.SectionId);
title = $"{PathHelper.CleanFileName(sectinfo.Name)} Untitled Page";
}

string filename;
if (title.Trim().Length > 0)
if (useUnderscores)
{
filename = Path.Combine(path, title + ext);
title = title.Replace(' ', '_');
}
else

// cleaned, sized, and ready go!
var filename = PathHelper.GetUniqueQualifiedFileName(path, ref title, ext);
if (filename is not null)
{
var pageinfo = await one.GetPageInfo(pageID);
var sectinfo = await one.GetSectionInfo(pageinfo.SectionId);
title = $"{PathHelper.CleanFileName(sectinfo.Name)} Untitled Page";
progress.SetMessage(filename);
progress.Increment();

if (useUnderscores)
if (format == OneNote.ExportFormat.HTML)
{
title = title.Replace(' ', '_');
if (withAttachments)
{
await archivist.ExportHTML(page, filename);
}
else
{
await archivist.Export(
page.PageId, filename, OneNote.ExportFormat.HTML);
}
}

filename = PathHelper
.GetUniqueQualifiedFilename(Path.Combine(path, title + ext));
}

progress.SetMessage(filename);
progress.Increment();

if (format == OneNote.ExportFormat.HTML)
{
if (withAttachments)
else if (format == OneNote.ExportFormat.XML)
{
archivist.ExportXML(page.Root, filename, withAttachments);
}
else if (format == OneNote.ExportFormat.Markdown)
{
_ = await archivist.ExportHTML(page, filename);
archivist.ExportMarkdown(page, filename, withAttachments);
}
else
{
await archivist.Export(page.PageId, filename, OneNote.ExportFormat.HTML);
await archivist.Export(
page.PageId, filename, format, withAttachments, embedded);
}
}
else if (format == OneNote.ExportFormat.XML)
{
archivist.ExportXML(page.Root, filename, withAttachments);
}
else if (format == OneNote.ExportFormat.Markdown)
{
archivist.ExportMarkdown(page, filename, withAttachments);

savedCount++;
}
else
{
await archivist.Export(page.PageId, filename, format, withAttachments, embedded);
logger.WriteLine($"export path too long [{path}\\{title}{ext}]");
}
}
}

SaveDefaultPath(path);

ShowMessage(string.Format(Resx.SaveAsMany_Success, pageIDs.Count, path));
ShowMessage(string.Format(Resx.SaveAsMany_Success, savedCount, pageIDs.Count, path));
}


private void SaveDefaultPath(string path)
private static void SaveDefaultPath(string path)
{
var provider = new SettingsProvider();
var settings = provider.GetCollection("Export");
Expand Down
Loading

0 comments on commit d050eb0

Please sign in to comment.