技术:Phoenix 1.4.9 问题:在带有“img”标签的网页中提供上传的图像,导致一些能够加载,而另一些则不能。
尝试检查图像链接,但图像链接(ae /image/1 或 /image/2)每次加载都没有问题。
强制缓存清除也不能证明问题的任何迹象。
如果人们想自己玩代码,这里是 git repo:https ://github.com/WannesFransen1994/phoenix-dynamic-images
我认为重要的代码位:控制器:
def index(conn, _params) do
images = Repo.all(Image)
render(conn, "index.html", images: images)
end
def create(conn, %{"upload" => %Plug.Upload{} = up}) do
{:ok, _u} = up |> ImageContext.create_image()
redirect(conn, to: Routes.page_path(conn, :index))
end
def display(conn, %{"id" => id}) do
i = Repo.get(Image, id)
conn |> put_resp_content_type(i.content_type) |> send_file(200, Image.local_path(i))
end
ImageContext.ex
def create_image(%{filename: _, path: tmp_path, content_type: _} = upload) do
hash = File.stream!(tmp_path, [], 2048) |> Image.sha256()
with {:ok, %File.Stat{size: size}} <- File.stat(tmp_path),
data_merged <- Map.from_struct(upload) |> Map.merge(%{size: size, hash: hash}),
{:ok, upload_cs} <- %Image{} |> Image.changeset(data_merged) |> Repo.insert(),
:ok <- tmp_path |> File.cp(Image.local_path(upload_cs)) do
{:ok, upload_cs}
else
{:error, reason} -> Repo.rollback(reason)
end
end
图像架构(没有变更集等......)
schema "images" do
field :filename, :string
field :content_type, :string
field :hash, :string
field :size, :integer
end
def local_path(%Image{} = upload) do
[@upload_directory, "#{upload.id}-#{upload.filename}"] |> Path.join()
end
输出(在图像显示链接上)有效,但是当我转到生成所有带有“img”标签的图像的概览页面时,它们随机无法加载(有时它们都工作,有时没有,有时一半,有时一个有效,其他无效)
奇怪的是,当您检查日志时,您会收到以下错误:
** (exit) an exception was raised:
** (File.Error) could not read file stats "uploads/images/1-user_upload_3.png": no such file or directory
当文件在那里并且当您重新加载或查看图像时它可以工作。