shahryarjb

shahryarjb

Hello, I am creating a LMS that lets the user to download file. The user need the file should send me his token and after validating I let him download whatever he needs.
Now how can I stop user from downloading directly? because files are uploaded by admin in ftp, and ftp path is in my server

I don’t know where I can start?

Thanks

Showing Posts 1 to 10

wolfiton

wolfiton

You can start by storing your files in your application in the assets folder then add authorization that only one type of user can download unlimited that file(ex: admin can download but user can’t).

If this scenario doesn’t work, then you can create a field in the user tables called plan. When a user pays you update the plan to paid. Then create a rule that only users with a paid plan can download files.

If you want to be even more specific then you will have to create another table with all the files structure like this:

  • id
  • file_name

Then create a join between the user tables with the files tables and verify each user to what file they belong

Also you will have to setup a payment system paypal stripe etc.

Also this link may provide some help Phoenix file download

outlog

outlog

I would “proxy” the download through phoenix..

user buys product - you persist purchase in DB
user hits /download_file/#{file_id}
phoenix verifies the login/session and purchase and then reads the file (or streams it) and sends it to the user

if you have multiple files just show a list of files and then the user can download on /download_file/#{file_id}

that way the user never gets a direct link and if the user shares the link /download_file/#{file_id} - the “stranger” clicking it will fail on having a session/logged in - and will fail on the purchase.. so it doesnt work..

shahryarjb

shahryarjb OP

I think I need this lines of code. this is the place I can start.

conn |> put_resp_header("content-disposition", ~s(attachment; filename="#{filename}")) |> send_file(200, filename)
shahryarjb

shahryarjb OP

would you mind explaining me more pleas? the stream file can be attractive, I haven’t used it before (streams)

cnck1387

cnck1387

To protect files in the past (and will be likely how I will do it in an upcoming Phoenix app) would be similar to how I do it with other tech stacks. I let nginx deal with it for the most part, while still doing authentication / authorization within Phoenix.

I’d keep the protected files outside of the main static assets folder.

Then have an “internal” location block in my nginx config like this:

  location /supersecrets/download/ {
    internal;
    alias /supersecrets/;
  }

Then for the download route at the Phoenix level, I’d authorize the download with a token and before I send the response, I’d set the Content-Disposition, X-Accel-Redirect and Content-Type headers which basically tells Phoenix to let nginx serve the file.

Here’s what that looks like in Python:

    response.headers['Content-Disposition'] = "attachment; filename=foo.zip"
    response.headers['X-Accel-Redirect'] = '/supersecrets/download/foo.zip'
    response.headers['Content-Type'] = 'application/octet-stream'

Of course you can change foo.zip to whatever file you’re serving.

I used this exact strategy like 5 years ago to serve a bunch of protected content and it held up very well.

shahryarjb

shahryarjb OP

Hello, Thanks
I use this code:

  def download(conn, _params) do
    file = File.read!("/Users/test/name.png")

    conn
     |> put_resp_content_type("image/png")
     |> put_resp_header("Content-disposition","attachment; filename=\"test.png\"")
     |> put_resp_header("X-Accel-Redirect", "/tempfile/download/test.png")
     |> put_resp_header("Content-Type", "application/octet-stream")
     |> send_resp(200, file)
  end

I have a question , how can use File.stream! instead of File.read!. I want to create chunks what I need and send to my user

wolfiton

wolfiton

ASfter a google search found this

https://code.tutsplus.com/tutorials/working-with-file-system-in-elixir--cms-28869

Sample code from the first link:

iex> stream = File.stream!("haiku.txt")
%File.Stream{
  line_or_bytes: :line,
  modes: [:raw, :read_ahead, :binary],
  path: "haiku.txt",
  raw: true
}
shahryarjb

shahryarjb OP

I saw these link before and test, but I had a error when I wanted to send file

file = stream = File.stream!("test.png")

    conn
     |> put_resp_content_type("image/png")
     |> put_resp_header("Content-disposition","attachment; filename=\"test.png\"")
     |> put_resp_header("X-Accel-Redirect", "/tempfile/download/test.png")
     |> put_resp_header("Content-Type", "application/octet-stream")
     |> send_resp(200, file)

send_resp has this error

no function clause matching in Plug.Conn.resp/3

if I change |> send_resp(200, file.path) it has no error but I couldn’t to create my chunk size and send to my user, and is it my file streams?


what about chunk?

I tested this code:

File.stream!("haiku.txt") |> Enum.with_index |> Enum.each(fn {content, _index} ->
****
end)

but I don’t know how can I change this line |> send_resp(200, file) that sends stream file to user download manager

wolfiton

wolfiton

Do you have a controller? or are you just trying to use static files?

Also here is a complete discussion on this Provide send_attachment/3 · Issue #1786 · phoenixframework/phoenix · GitHub

Try the

send_chunked(200)
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Seems like Plug.Conn — Plug v1.20.2 is the right call here. I see that this was tried earlier, what about it didn’t work?

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews