-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from bluzky/import-export
Import export
- Loading branch information
Showing
35 changed files
with
381 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
defmodule Chubi.ContentFileStore do | ||
alias Chubi.Content.Post | ||
alias Chubi.Content.Page | ||
@content_dir "./priv/content" | ||
|
||
def content_directory(suffix \\ "") do | ||
@content_dir | ||
|> Path.join(suffix) | ||
|> Path.absname() | ||
end | ||
|
||
def ensure_directory(path) do | ||
if File.dir?(path) do | ||
path | ||
else | ||
File.mkdir_p!(path) | ||
path | ||
end | ||
end | ||
|
||
defp directory(post) do | ||
case post do | ||
%Post{} -> | ||
ensure_directory(content_directory("posts")) | ||
|
||
%Page{} -> | ||
ensure_directory(content_directory("pages")) | ||
|
||
_ -> | ||
nil | ||
end | ||
end | ||
|
||
defp extension(post) do | ||
case post.format do | ||
"markdown" -> "md" | ||
_ -> "json" | ||
end | ||
end | ||
|
||
defp content(%Post{} = post) do | ||
case post.format do | ||
"markdown" -> | ||
post.content | ||
|
||
_ -> | ||
post | ||
|> Map.drop([:__meta__, :__struct__]) | ||
|> Map.merge(%{ | ||
categories: Enum.map(post.categories, & &1.name), | ||
tags: Enum.map(post.tags, & &1.name) | ||
}) | ||
|> Jason.encode!() | ||
end | ||
end | ||
|
||
defp content(%Page{} = post) do | ||
case post.format do | ||
"markdown" -> | ||
post.content | ||
|
||
_ -> | ||
post | ||
|> Map.drop([:__meta__, :__struct__]) | ||
|> Jason.encode!() | ||
end | ||
end | ||
|
||
defp file_name(post) do | ||
date = Timex.format!(post.date || post.inserted_at, "{YYYY}-{0M}-{0D}") | ||
"#{date}-#{post.slug}.#{extension(post)}" | ||
end | ||
|
||
defp file_path(post) do | ||
Path.join(directory(post), file_name(post)) | ||
end | ||
|
||
def write(post) do | ||
File.write(file_path(post), content(post), [:write]) | ||
end | ||
|
||
def write_mem(post) do | ||
{file_name(post), content(post)} | ||
end | ||
|
||
def delete(post) do | ||
File.rm(file_path(post)) | ||
end | ||
|
||
def read_all_posts() do | ||
ensure_directory(content_directory("/posts")) | ||
|> read_directory | ||
end | ||
|
||
def read_all_pages() do | ||
ensure_directory(content_directory("/pages")) | ||
|> read_directory | ||
end | ||
|
||
def read_directory(path) do | ||
with {:ok, files} <- File.ls(path) do | ||
Enum.map(files, fn file -> | ||
Path.join(path, file) | ||
|> read_file | ||
end) | ||
|> Enum.filter(&(not is_nil(&1))) | ||
else | ||
_ -> [] | ||
end | ||
end | ||
|
||
def read_file(file_path) do | ||
with {:ok, content} <- File.read(file_path) do | ||
case Path.extname(file_path) do | ||
".md" -> %{"content" => content, "format" => "markdown"} | ||
".json" -> Jason.decode!(content) | ||
_ -> nil | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule ChubiWeb.AdminRouter do | ||
use ChubiWeb, :router | ||
|
||
pipeline :browser do | ||
plug(:accepts, ["html"]) | ||
plug(:fetch_session) | ||
plug(:fetch_flash) | ||
plug(:protect_from_forgery) | ||
plug(:put_secure_browser_headers) | ||
end | ||
|
||
pipeline :admin do | ||
plug(:put_layout, {ChubiWeb.Admin.LayoutView, "app.html"}) | ||
plug(BasicAuth, use_config: {:chubi, :auth_config}) | ||
plug(ChubiWeb.Plugs.PutSiteSetting) | ||
plug(ChubiWeb.Plugs.LoadSiteParams) | ||
end | ||
|
||
pipeline :preview do | ||
plug(BasicAuth, use_config: {:chubi, :auth_config}) | ||
end | ||
|
||
pipeline :app do | ||
plug(ChubiWeb.Plugs.PutSiteSetting) | ||
plug(ChubiWeb.Plugs.LoadSiteParams) | ||
# put this at end of pipeline | ||
plug(ChubiWeb.Plugs.PutSiteLayout) | ||
end | ||
|
||
scope "/", ChubiWeb.Admin, path: "/admin" do | ||
pipe_through([:browser, :admin]) | ||
get("/", PostController, :index) | ||
resources("/tags", TagController) | ||
resources("/categories", CategoryController) | ||
resources("/posts", PostController) | ||
resources("/pages", PageController) | ||
|
||
post("/upload", UploadController, :create) | ||
delete("/upload", UploadController, :delete) | ||
|
||
get("/settings/set_theme", SettingController, :set_theme) | ||
get("/settings/export", SettingController, :export_content) | ||
get("/settings/select_import", SettingController, :select_import) | ||
post("/settings/import", SettingController, :import_content) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,86 @@ | ||
defmodule ChubiWeb.Admin.SettingController do | ||
use ChubiWeb, :controller | ||
use ChubiWeb, :admin_controller | ||
alias Chubi.Content | ||
|
||
def set_theme(conn, %{"theme" => theme}) do | ||
conn | ||
|> put_session(:theme, theme) | ||
|> redirect(to: Routes.admin_post_path(conn, :index)) | ||
|> redirect(to: Routes.post_path(conn, :index)) | ||
end | ||
|
||
def export_content(conn, _params) do | ||
posts = | ||
Content.list_posts() | ||
|> Enum.map(&Chubi.ContentFileStore.write_mem(&1)) | ||
|> Enum.map(fn {file_name, content} -> | ||
{String.to_charlist(Path.join("posts/", file_name)), content} | ||
end) | ||
|
||
pages = | ||
Content.list_pages() | ||
|> Enum.map(&Chubi.ContentFileStore.write_mem(&1)) | ||
|> Enum.map(fn {file_name, content} -> | ||
{String.to_charlist(Path.join("pages/", file_name)), content} | ||
end) | ||
|
||
with {:ok, {filename, data}} = :zip.create("blog-content.zip", posts ++ pages, [:memory]) do | ||
# download file | ||
conn | ||
|> Plug.Conn.put_resp_content_type("application/octet-stream") | ||
|> Plug.Conn.put_resp_header( | ||
"content-disposition", | ||
"attachment; filename=\"#{filename}\"" | ||
) | ||
|> Plug.Conn.send_resp(:ok, data) | ||
else | ||
_ -> | ||
conn | ||
|> put_flash(:error, gettext("Cannot generate template")) | ||
|> redirect(to: Routes.post_path(conn, :index)) | ||
end | ||
end | ||
|
||
def select_import(conn, _) do | ||
render(conn, "select_import.html") | ||
end | ||
|
||
def import_content(conn, %{"file" => %{path: path}}) do | ||
with {:ok, files} <- :zip.extract(String.to_charlist(path), [:memory]) do | ||
rs = | ||
Enum.map(files, fn {file_path, content} -> | ||
file_path = | ||
to_string(file_path) | ||
|> IO.inspect() | ||
|
||
attrs = | ||
case Path.extname(file_path) do | ||
".md" -> | ||
%{"content" => content, "format" => "markdown"} | ||
|
||
_ext -> | ||
Jason.decode!(content) | ||
end | ||
|
||
if String.starts_with?(file_path, "posts") do | ||
Content.create_post(attrs) | ||
else | ||
Content.create_page(attrs) | ||
end | ||
end) | ||
|
||
success = Enum.filter(rs, &(elem(&1, 0) == :ok)) | ||
|
||
conn | ||
|> put_flash( | ||
:info, | ||
gettext("%{success}/%{total} is imported", %{success: length(success), total: length(rs)}) | ||
) | ||
|> redirect(to: Routes.post_path(conn, :index)) | ||
else | ||
err -> | ||
conn | ||
|> put_flash(:error, gettext("Cannot import content")) | ||
|> redirect(to: Routes.setting_path(conn, :select_import)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.