implement creation of link on first step in backend
This commit is contained in:
parent
04225c44aa
commit
89c2a99283
|
@ -44,31 +44,3 @@ window.liveSocket = liveSocket
|
|||
window.Components = {
|
||||
SplashPage, JustPage, ForPage, YouPage
|
||||
}
|
||||
|
||||
// const root_el = document.getElementById("root");
|
||||
// const page = root_el.getAttribute("page")
|
||||
// let result;
|
||||
|
||||
// switch(page) {
|
||||
// case "splash":
|
||||
// result = <SplashPage />
|
||||
// break;
|
||||
// case "just":
|
||||
// result = <JustPage />
|
||||
// break;
|
||||
// case "for":
|
||||
// case "you":
|
||||
// case "identify":
|
||||
// case "revealed":
|
||||
// default:
|
||||
// null
|
||||
// break;
|
||||
// }
|
||||
|
||||
// ReactDOM.render(<React.StrictMode>
|
||||
// <GlobalStyle />
|
||||
// { result }
|
||||
// </React.StrictMode>
|
||||
// ,
|
||||
// root_el
|
||||
// );
|
|
@ -7,7 +7,6 @@ const JustPage = () => {
|
|||
const [secretInput, setSecretInput] = useState("");
|
||||
const [fileInput, setFileInput] = useState<File | null>(null);
|
||||
const [fileName, setFileName] = useState("");
|
||||
const [encryptedSecret, setEncryptedSecret] = useState("");
|
||||
|
||||
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||
setSecretInput(e.target.value);
|
||||
|
@ -45,18 +44,13 @@ const JustPage = () => {
|
|||
encoded
|
||||
);
|
||||
|
||||
// encrypted will be sent to lumen backend
|
||||
HexMix.arrayBufferToString(encrypted, (result: string) => {
|
||||
setEncryptedSecret(result);
|
||||
});
|
||||
|
||||
const keyHex = HexMix.uint8ToHex(new Uint8Array(exported));
|
||||
const ivHex = HexMix.uint8ToHex(iv);
|
||||
|
||||
const formData = new FormData();
|
||||
const blobData = new Blob([encrypted]);
|
||||
|
||||
formData.append('blob', blobData);
|
||||
formData.append('text_content', blobData);
|
||||
formData.append('filetype', 'text/plain');
|
||||
formData.append('filename', 'secret.txt');
|
||||
|
||||
|
@ -94,7 +88,6 @@ const JustPage = () => {
|
|||
</TextAlignWrapper>
|
||||
<Spacer space="1.6rem" />
|
||||
<FileInput id="fileInput" value={fileName} handleFile={handleFile} />
|
||||
{ encryptedSecret ? "" : encryptedSecret }
|
||||
{ fileInput ? "" : ""}
|
||||
<Spacer space="4rem" />
|
||||
<div
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
export default {
|
||||
uint8ToHex(data: Uint8Array): string {
|
||||
return Array.prototype.map.call(data, (byte: number) => {
|
||||
return ('00' + byte.toString(16)).slice(-2);
|
||||
}).join('');
|
||||
},
|
||||
hexToUint8(data: string): Uint8Array {
|
||||
const hexArray = data.match(/.{1,2}/g);
|
||||
if (!hexArray) return new Uint8Array(0);
|
||||
|
||||
return Uint8Array.from(hexArray.map((char: string) => {
|
||||
return parseInt(char, 16)
|
||||
}));
|
||||
},
|
||||
arrayBufferToString(buf: ArrayBuffer, callback: Function) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = function (event: ProgressEvent<FileReader>) {
|
||||
callback(event?.target?.result);
|
||||
};
|
||||
reader.onerror = function (event: ProgressEvent<FileReader>) {
|
||||
alert(event?.target?.error);
|
||||
};
|
||||
reader.readAsBinaryString(new Blob([ buf ],
|
||||
{ type: 'application/octet-stream' }));
|
||||
},
|
||||
stringToArrayBuffer(str: string) {
|
||||
const array = new Uint8Array(str.length);
|
||||
for(let i = 0; i < str.length; i++) {
|
||||
array[i] = str.charCodeAt(i);
|
||||
}
|
||||
return array.buffer
|
||||
}
|
||||
}
|
|
@ -5,6 +5,10 @@ defmodule Entendu.Links.Link do
|
|||
schema "links" do
|
||||
field :burn_after_reading, :boolean, default: false
|
||||
field :expires, :utc_datetime
|
||||
field :filename, :string
|
||||
field :filetype, :string
|
||||
field :text_content, :string
|
||||
field :file_content, :string
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
@ -12,7 +16,7 @@ defmodule Entendu.Links.Link do
|
|||
@doc false
|
||||
def changeset(link, attrs) do
|
||||
link
|
||||
|> cast(attrs, [:expires, :burn_after_reading])
|
||||
|> validate_required([:expires, :burn_after_reading])
|
||||
|> cast(attrs, [:expires, :burn_after_reading, :filename, :filetype, :text_content, :file_content])
|
||||
|> validate_required([:expires, :burn_after_reading, :filename, :filetype])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,22 +4,40 @@ defmodule EntenduWeb.LinkController do
|
|||
"""
|
||||
|
||||
use EntenduWeb, :controller
|
||||
use Params
|
||||
|
||||
alias Entendu.Links
|
||||
alias Links.Link
|
||||
alias Ecto.Changeset
|
||||
|
||||
def just_page(conn, _params) do
|
||||
render(conn, "just.html")
|
||||
end
|
||||
|
||||
def just(conn, %{encrypted_contents: contents}) do
|
||||
conn
|
||||
|> put_session(:encrypted_contents, contents)
|
||||
|> redirect(to: "/just/for")
|
||||
defparams first_step %{
|
||||
burn_after_reading: [field: :boolean, default: false],
|
||||
expires: :utc_datetime,
|
||||
filename: :string,
|
||||
filetype: :string,
|
||||
text_content: :string,
|
||||
file_content: :string
|
||||
}
|
||||
|
||||
def just(conn, params) do
|
||||
with %Changeset{valid?: true} = changeset <- first_step(params),
|
||||
link_params <- Params.to_map(changeset),
|
||||
{:ok, %Link{} = link} <- Links.create_link(link_params) do
|
||||
conn
|
||||
|> assign(:link, link)
|
||||
|> render("show_authorized", link: link)
|
||||
end
|
||||
end
|
||||
|
||||
def for_page(conn, _params) do
|
||||
render(conn, "for.html")
|
||||
end
|
||||
|
||||
def for(conn, %{username: username, service: service}) do
|
||||
def for(_conn, %{username: _username, service: _service}) do
|
||||
{:error, "not implemented"}
|
||||
end
|
||||
|
||||
|
|
|
@ -1,3 +1,22 @@
|
|||
defmodule EntenduWeb.LinkView do
|
||||
use EntenduWeb, :view
|
||||
alias EntenduWeb.LinkView
|
||||
|
||||
def render("show_authorized.json", %{link: link}) do
|
||||
%{
|
||||
id: link.id,
|
||||
burn_after_reading: link.burn_after_reading,
|
||||
expires: link.expires,
|
||||
filename: link.filename,
|
||||
filetype: link.filetype,
|
||||
text_content: link.text_content,
|
||||
file_content: link.file_content
|
||||
}
|
||||
end
|
||||
|
||||
def render("show_unauthorized.json", %{link: link}) do
|
||||
%{
|
||||
id: link.id
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
3
mix.exs
3
mix.exs
|
@ -50,7 +50,8 @@ defmodule Entendu.MixProject do
|
|||
{:libcluster, "~> 3.2"},
|
||||
{:ueberauth, "~> 0.7.0"},
|
||||
{:ueberauth_github, "~> 0.8.1"},
|
||||
{:react_phoenix, "~> 1.3"}
|
||||
{:react_phoenix, "~> 1.3"},
|
||||
{:params, "~> 2.2"}
|
||||
]
|
||||
end
|
||||
|
||||
|
|
1
mix.lock
1
mix.lock
|
@ -20,6 +20,7 @@
|
|||
"mime": {:hex, :mime, "2.0.2", "0b9e1a4c840eafb68d820b0e2158ef5c49385d17fb36855ac6e7e087d4b1dcc5", [:mix], [], "hexpm", "e6a3f76b4c277739e36c2e21a2c640778ba4c3846189d5ab19f97f126df5f9b7"},
|
||||
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
|
||||
"oauth2": {:hex, :oauth2, "2.0.0", "338382079fe16c514420fa218b0903f8ad2d4bfc0ad0c9f988867dfa246731b0", [:mix], [{:hackney, "~> 1.13", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "881b8364ac7385f9fddc7949379cbe3f7081da37233a1aa7aab844670a91e7e7"},
|
||||
"params": {:hex, :params, "2.2.0", "2fa495b1123ad0bdfeba44dcfb2738d99b6d933fd026ff1b8c36761decd1353a", [:mix], [{:ecto, "~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}], "hexpm", "22262ecff055995e4283f32c05f105f0727a07227bf95f4cf24629c7271d2689"},
|
||||
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
|
||||
"phoenix": {:hex, :phoenix, "1.5.10", "3ee7d5c17ff9626d72d374d8fc8909bf00f4323fd15549fbe3abbbd38b5299c8", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.13 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.1.2 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f9c2eaa5a8fe5a412610c6aa84ccdb6f3e92f333d4df7fbaeb0d5a157dbfb48d"},
|
||||
"phoenix_ecto": {:hex, :phoenix_ecto, "4.3.0", "2c69a452c2e0ee8c93345ae1cdc1696ef4877ff9cbb15c305def41960c3c4ebf", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "0ac491924217550c8f42c81c1f390b5d81517d12ceaf9abf3e701156760a848e"},
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
defmodule Entendu.Repo.Migrations.AddFilenameToLinks do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
alter table(:links) do
|
||||
remove_if_exists(:content, :string)
|
||||
add :filename, :string
|
||||
add :filetype, :string
|
||||
add :text_content, :string
|
||||
add :file_content, :string
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue