aboutsummaryrefslogtreecommitdiff
path: root/back-end/src/ImportExportUtil.cs
blob: 3610ef61128b5119cbea0513b8aa5ac88e72564f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (C) 2024 Vaughn Nugent
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

using System;
using System.IO;
using SimpleBookmark.Model;
using System.Collections.Generic;
using VNLib.Utils.IO;



namespace SimpleBookmark
{
    internal static class ImportExportUtil
    {
        /// <summary>
        /// Exports a colletion of bookmarks to a netscape bookmark file
        /// </summary>
        /// <param name="bookmarks">The bookmark collection to export</param>
        /// <param name="outputStream">The bookmark output stream to write data to</param>
        public static void ExportToNetscapeFile(IEnumerable<BookmarkEntry> bookmarks, Stream outputStream)
        {
            using VnStreamWriter writer = new(outputStream, System.Text.Encoding.UTF8, 1024)
            {
                NewLine = "\r\n"
            };

            writer.WriteLine("<!DOCTYPE NETSCAPE-Bookmark-file-1>");
            writer.WriteLine("<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">");
            writer.WriteLine("<TITLE>Bookmarks</TITLE>");
            writer.WriteLine("<H1>Bookmarks</H1>");
            writer.WriteLine("<DL><p>");

            //Add each bookmark
            foreach (BookmarkEntry entry in bookmarks)
            {
                writer.Write("<DT><A HREF=\"");
                writer.Write(entry.Url);

                writer.Write("\" ADD_DATE=\"");
                writer.Write(new DateTimeOffset(entry.Created).ToUnixTimeSeconds());

                writer.Write("\" LAST_VISIT=\"");
                writer.Write(new DateTimeOffset(entry.LastModified).ToUnixTimeSeconds());

                //tags
                writer.Write("\" TAGS=\"");
                writer.Write(entry.Tags);

                writer.Write("\">");
                writer.Write(entry.Name);
                writer.WriteLine("</A>");

                //description
                writer.Write("<DD>");
                writer.WriteLine(entry.Description);

            }

            //Close document
            writer.WriteLine("</DL></p>");
            writer.Flush();
        }
    }
}