Coder Social home page Coder Social logo

upload2's Introduction

upload2

Railsで作ったファイルアップローダ

仕様

http://rosylilly.hatenablog.com/entry/2013/10/21/190019

DBにファイルの内容ごと保存する仕様。

ファイル毎のURLはファイルのIDにHashidsをかけたものにして、多少はプライベートモードが意味を成すようにした。

Railsの設定

  • テンプレートはHaml
  • テストはMinitest
  • JavaScriptのビルドはWebpacker
    • CSSを書きたくなかったのでMaterialize-CSSを使った
  • Turbolinks、Sprocketsその他使わなそうなものは無効

DBの設計

file_infos

ファイルの情報を持つテーブル。以下のカラムを持つ。

  • name: ファイル名
  • content_size: ファイルの大きさ
  • private: プライベートモードかどうか
  • expiration: 有効期限。NULLの場合は無期限を表す。
  • password_digest: 削除用のパスワードのハッシュ値

加えて、id, created_at, updated_atも持つ。

created_at, private, expirationの複合インデックスが貼られていて、これはファイルの一覧を取得するためのクエリに利用される。

file_contents

ファイルの内容を持つテーブル。以下のカラムを持つ。

  • file_info_id: 対応するfile_infoid
  • content: ファイルの内容

加えて、id, created_at, updated_atも持つ。

file_infosfile_contentsを分けたのにそこまで深い意図があるわけではないのだけど、理由としては、

  • 将来的にファイルの中身をS3などに保存することになったときに拡張しやすそう。
  • selectを書かなくてもパフォーマンス的に問題にならなそう。

くらいのゆるっとした感覚がある。

テスト

少なくとも手元で動かしたときにカバレッジが100%になる程度には書いた。ただRailsのfixturesの使い方があれでいいのかよく分かっていない。Railsは何も分からない。

System Testも書いたのでbin/rails test:systemとするとブラウザが立ち上がって、ファイルをアップロード→削除までの一連の流れをテストできる。

思うところ

upload2という名前が表す通り、これはファイルアップローダの仕様を一通り実装し終えたあとに、もう一度作り直したもの。とりあえず満足のいくものになったような気がする。

Webpackerは思ったよりも使いやすかった。Sprocketsでやってるならそれでいいと思うけど、新しくやるなら悪くない選択肢だと思う。大して使ってないから分からないけど。

アップロードのWeb画面で日付しか設定できないのは完全に手抜きで、Materialize-CSSが日付と時刻を一度に設定するダイアログに対応していなかったからというだけの理由。直した方がいいのだけど面倒なのでやっていない。

CIとかは気が向いたら設定する。

upload2's People

Contributors

makenowjust avatar

Stargazers

Kent Gruber avatar

Watchers

James Cloos avatar  avatar  avatar

upload2's Issues

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper integration’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.