Commit Graph

58 Commits

Author SHA1 Message Date
evazion
abdab7a0a8 uploads: rework upload process.
Rework the upload process so that files are saved to Danbooru first
before the user starts tagging the upload.

The main user-visible change is that you have to select the file first
before you can start tagging it. Saving the file first lets us fix a
number of problems:

* We can check for dupes before the user tags the upload.
* We can perform dupe checks and show preview images for users not using the bookmarklet.
* We can show preview images without having to proxy images through Danbooru.
* We can show previews of videos and ugoira files.
* We can reliably show the filesize and resolution of the image.
* We can let the user save files to upload later.
* We can get rid of a lot of spaghetti code related to preprocessing
  uploads. This was the cause of most weird "md5 confirmation doesn't
  match md5" errors.

(Not all of these are implemented yet.)

Internally, uploading is now a two-step process: first we create an upload
object, then we create a post from the upload. This is how it works:

* The user goes to /uploads/new and chooses a file or pastes an URL into
  the file upload component.
* The file upload component calls `POST /uploads` to create an upload.
* `POST /uploads` immediately returns a new upload object in the `pending` state.
* Danbooru starts processing the upload in a background job (downloading,
  resizing, and transferring the image to the image servers).
* The file upload component polls `/uploads/$id.json`, checking the
  upload `status` until it returns `completed` or `error`.
* When the upload status is `completed`, the user is redirected to /uploads/$id.
* On the /uploads/$id page, the user can tag the upload and submit it.
* The upload form calls `POST /posts` to create a new post from the upload.
* The user is redirected to the new post.

This is the data model:

* An upload represents a set of files uploaded to Danbooru by a user.
  Uploaded files don't have to belong to a post. An upload has an
  uploader, a status (pending, processing, completed, or error), a
  source (unless uploading from a file), and a list of media assets
  (image or video files).

* There is a has-and-belongs-to-many relationship between uploads and
  media assets. An upload can have many media assets, and a media asset
  can belong to multiple uploads. Uploads are joined to media assets
  through a upload_media_assets table.

  An upload could potentially have multiple media assets if it's a Pixiv
  or Twitter gallery. This is not yet implemented (at the moment all
  uploads have one media asset).

  A media asset can belong to multiple uploads if multiple people try
  to upload the same file, or if the same user tries to upload the same
  file more than once.

New features:

* On the upload page, you can press Ctrl+V to paste an URL and immediately upload it.
* You can save files for upload later. Your saved files are at /uploads.

Fixes:

* Improved error messages when uploading invalid files, bad URLs, and
  when forgetting the rating.
2022-01-28 04:13:22 -06:00
evazion
8f36ebe2b8 Fix #4914: RuntimeError corrupting uploads
Bug: If a media asset got stuck in the 'processing' state during upload,
then it would stay stuck forever and the file couldn't be uploaded again
later.

Fix: Mark stuck assets as failed before raising the "Upload failed"
error. Once the asset is marked as failed, it can be uploaded again
later. Also, only wait for assets to finish processing if they were
uploaded less than 5 minutes ago. If a processing asset is more than 5
minutes old, consider it stuck and mark it as failed immediately.

Assets getting stuck in the processing state is a 'this should never
happen' error. Normally if any kind of exception is raised while
uploading the asset, the asset will be set to the 'failed' state. The
only way an asset can get stuck is if it fails and the exception handler
doesn't run, or the exception handler itself fails. This might happen if
the process is unexpectedly killed, or possibly if the HTTP request
times out and a TimeoutError is raised at an inopportune time. See below
for discussion of issues with Timeout.

[1]: https://vaneyckt.io/posts/the_disaster_that_is_rubys_timeout_method/
[2]: https://jvns.ca/blog/2015/11/27/why-rubys-timeout-is-dangerous-and-thread-dot-raise-is-terrifying/
[3]: https://adamhooper.medium.com/in-ruby-dont-use-timeout-77d9d4e5a001
[4]: https://ruby-doc.org/core-3.0.2/Thread.html#method-c-handle_interrupt-label-Guarding+from+Timeout-3A-3AError
2021-11-08 18:22:04 -06:00
evazion
ac12efb636 tests: fix test failures when running without API keys.
Fix the test suite failing when trying to run it in the default state
with no config file or API keys configured. Most source sites require
API keys or login credentials to be set in order to work. Skip these
tests when credentials aren't configured.
2021-09-22 04:33:36 -05:00
evazion
0f36bbf8d3 iqdb: update API client to use new version of IQDB.
Replace the old IQDB API client with a new client for the new forked
version of IQDB at https://github.com/danbooru/iqdb.

Changes:

* The /iqdb_queries endpoint now returns `hash` and `signature` fields.
  The `signature` is the full decoded Haar signature, while the `hash`
  is a encoded version of the signature.
* The /iqdb_queries endpoint no longer returns `width` and `height`
  fields in the response (these were always 128x128).
* We no longer need the IQDBs frontend server, now we talk to the IQDB
  instance directly.
* We no longer send add/remove image commands to IQDB through AWS SQS,
  now we send them to IQDB directly. They are sent in a delayed job so
  that if IQDB is down, uploading images is still possible, the add
  image commands will just get queued up.
* Fix a bug where regenerating an image's thumbnails didn't regenerate
  IQDB, because IQDB silently ignored add image commands when the image
  already existed in the database.
2021-06-16 05:36:24 -05:00
evazion
4deb8aeea2 uploads: disallow uploading new Flash files.
Flash is dead. It's no longer supported by browsers, it's not
well-supported by emulators, and only two Flash posts were uploaded in
the last year anyway. Old Flash files will continue to exist, but new
Flash uploads will no longer be allowed.
2021-03-31 20:47:35 -05:00
evazion
9d60046f1d tests: fixup typo in 520b72948. 2021-02-04 00:41:14 -06:00
evazion
520b72948f Fix #4695: Raise max video length to match Twitter's (2:20). 2021-02-04 00:01:03 -06:00
evazion
716cdb0126 tests: fix broken tests.
* Fix a broken Twitter profile image upload test.
* Skip a broken DeviantArt flash file upload test (flash no longer
  supported by DeviantArt?)
* Skip user upgrade tests when Stripe is not configured.
2021-01-27 00:40:53 -06:00
evazion
913ce88024 tests: factor out upload helper methods. 2021-01-04 20:15:37 -06:00
evazion
cc64f8b7ee tests: fix broken source tests.
Fix various tests broken by source files changing or being deleted.
2020-11-10 14:52:54 -06:00
evazion
1d2a8a7898 fanbox: don't raise error on age-restricted posts.
Prevent age-restricted fanbox posts from raising errors when source data
is fetched. This prevents error messages from being shown to users when
switching to the edit tab on a post.

This will cause uploads of age-restricted posts to fail with an
unrelated error because we either can't find the image url (if we were
given only the html page) or we can't download the image (because we're
not logged in to Fanbox).
2020-08-18 15:34:12 -05:00
BrokenEagle
a903bd95f9 Add tests for all models with includes searches 2020-07-27 19:29:22 +00:00
evazion
0c7d48d890 posts: autoban uploads tagged paid_reward. 2020-07-06 13:52:46 -05:00
evazion
85f58bf2f6 newgrounds: fix style nitpicks. 2020-06-24 00:25:45 -05:00
evazion
73506bac33 twitter: add tests for uploading profile banners (#4520). 2020-06-23 02:37:21 -05:00
evazion
8c6759bbd7 nicoseiga: fix login endpoint.
* Update the login endpoint. The old endpoint returns 404 now.

  POST https://account.nicovideo.jp/api/v1/login ->
  POST https://account.nicovideo.jp/login/redirector?site=seiga

* Let Danbooru::Http cache the login request instead of caching it manually.

* Let Danbooru::Http automatically follow redirects instead of dealing
  with the Location header manually.
2020-06-22 18:46:47 -05:00
evazion
db3407caa3 uploads: fix uploading from source not working.
ref: 26ad844bbe (r40077579).
2020-06-22 15:32:48 -05:00
evazion
29a5f7dfc8 image proxy: replace HTTParty with Danbooru::Http. 2020-06-21 15:11:56 -05:00
evazion
99a9149fc4 tests: eliminate as_user helper. 2020-06-11 01:02:01 -05:00
evazion
8b5ffb4c43 uploads: allow admins to upload videos more than 2 minutes long.
At some point the ability for admins to bypass the video length
restriction got lost.

ref: https://danbooru.donmai.us/forum_topics/14647
2020-06-09 03:08:06 -05:00
evazion
18ce552dae tests: fix upload controller tests.
We can't use the name `test_file_upload` because then the helper method
gets treated like it's a test case.
2020-06-02 18:25:45 -05:00
evazion
84cd8540ab tests: add tests for uploading flash files (#4484). 2020-05-27 14:26:52 -05:00
evazion
1e0f6f730a uploads: only let users see their own uploads on /uploads listing. 2020-04-06 14:13:22 -05:00
evazion
7f742242e4 pundit: convert uploads to pundit. 2020-03-20 18:03:01 -05:00
evazion
3c2a379d6f uploads: replace old upload limits with new upload limits. 2020-02-03 22:05:58 -06:00
evazion
79015b4341 tests: fix various broken tests. 2020-01-21 00:09:38 -06:00
evazion
309821bf73 rubocop: fix various style issues. 2019-12-22 21:23:37 -06:00
evazion
0a6661d145 uploads: switch to active job.
* Switch upload processing from DelayedJob to ActiveJob.
* Remove remaining references to delayed job from tests.

Closes #4128.
2019-09-23 15:11:18 -05:00
evazion
1e0aadb6e7 Fix /uploads?search[uploader_name] not filtering by uploader name.
Caused by `relation = self` in `search_post_id_attribute`.
2019-09-02 19:53:47 -05:00
evazion
59b277ead1 users: drop id_to_name, name_to_id caching.
Changes:

* Drop Users.id_to_name.
* Don't cache Users.name_to_id.
* Replace calls to name_to_id with find_by_name when possible.
* Don't autodefine creator_name in belongs_to_creator.
* Don't autodefine updater_name in belongs_to_updater.
* Instead manually define creator_name / updater_name only on models that need
  to return these fields in the api.

id_to_name was cached to reduce the impact of N+1 query patterns in
certain places, especially in api responses that return creator_name /
updater_name fields. But it still meant we were doing N calls to
memcache. Using `includes` to prefetch users avoids this N+1 pattern.

name_to_id had no need be cached, it was never used in any performance-
sensitive contexts.

Avoiding caching also avoids the need to keep these caches consistent.
2019-08-18 11:24:42 -05:00
Albert Yi
c18cf5480d update uploadcontroller test 2019-04-16 16:26:38 -07:00
Albert Yi
30a5d745a0 additional spec fixes 2018-09-04 14:01:35 -07:00
Albert Yi
762dc3da24 Refactor sources 2018-08-24 12:10:51 -07:00
Albert Yi
6ce8c72053 assign after source url for uploads 2018-07-31 10:10:50 -07:00
Albert Yi
77854349e5 testing 2018-07-26 18:11:19 -07:00
Albert Yi
867406f410 handle the case where a file is uploaded that doesn't match whatever was preprocessed 2018-07-02 14:47:23 -07:00
Albert Yi
c17e0baad0 prefer file over source when both are provided (#3767) 2018-06-25 14:00:02 -07:00
Albert Yi
fdd7582fb0 add support for upload preprocessing 2018-06-14 17:52:41 -07:00
evazion
e6ce05eb29 uploads: add more search options for /uploads listing (#3657). 2018-04-20 15:51:56 -05:00
evazion
8083c7daae uploads: fix batch upload page to work without bookmarklet.
* Add a form to the /uploads/batch page so that it can be used without
  the bookmarklet.

* Fix an exception when no url is given.
2018-04-20 15:51:56 -05:00
evazion
bca483cefd Fix #3628: Retry button is displayed for failed uploads that cannot succeed even if retried 2018-04-12 22:14:06 -05:00
r888888888
abce4d2551 Raise error on unpermitted params.
Fail loudly if we forget to whitelist a param instead of silently
ignoring it.

misc models: convert to strong params.

artist commentaries: convert to strong params.

* Disallow changing or setting post_id to a nonexistent post.

artists: convert to strong params.

* Disallow setting `is_banned` in create/update actions. Changing it
  this way instead of with the ban/unban actions would leave the artist in
  a partially banned state.

bans: convert to strong params.

* Disallow changing the user_id after the ban has been created.

comments: convert to strong params.

favorite groups: convert to strong params.

news updates: convert to strong params.

post appeals: convert to strong params.

post flags: convert to strong params.

* Disallow users from setting the `is_deleted` / `is_resolved` flags.

ip bans: convert to strong params.

user feedbacks: convert to strong params.

* Disallow users from setting `disable_dmail_notification` when creating feedbacks.
* Disallow changing the user_id after the feedback has been created.

notes: convert to strong params.

wiki pages: convert to strong params.

* Also fix non-Builders being able to delete wiki pages.

saved searches: convert to strong params.

pools: convert to strong params.

* Disallow setting `post_count` or `is_deleted` in create/update actions.

janitor trials: convert to strong params.

post disapprovals: convert to strong params.

* Factor out quick-mod bar to shared partial.
* Fix quick-mod bar to use `Post#is_approvable?` to determine visibility
  of Approve button.

dmail filters: convert to strong params.

password resets: convert to strong params.

user name change requests: convert to strong params.

posts: convert to strong params.

users: convert to strong params.

* Disallow setting password_hash, last_logged_in_at, last_forum_read_at,
  has_mail, and dmail_filter_attributes[user_id].

* Remove initialize_default_image_size (dead code).

uploads: convert to strong params.

* Remove `initialize_status` because status already defaults to pending
  in the database.

tag aliases/implications: convert to strong params.

tags: convert to strong params.

forum posts: convert to strong params.

* Disallow changing the topic_id after creating the post.
* Disallow setting is_deleted (destroy/undelete actions should be used instead).
* Remove is_sticky / is_locked (nonexistent attributes).

forum topics: convert to strong params.

* merges https://github.com/evazion/danbooru/tree/wip-rails-5.1
* lock pg gem to 0.21 (1.0.0 is incompatible with rails 5.1.4)
* switch to factorybot and change all references

Co-authored-by: r888888888 <r888888888@gmail.com>
Co-authored-by: evazion <noizave@gmail.com>

add diffs
2018-04-06 18:09:57 -07:00
evazion
4f543671a2 tests: move test/helpers to test/test_helpers.
The Rails convention is for test/helpers to be used for testing the view
helpers in app/helpers. We were using it to store certain utility
methods instead. Move these to test/test_helpers so that test/helpers
can be used for its intended purpose.
2018-01-14 16:11:15 -06:00
r888888888
014b9af5f8 fetch ugoiras for batch action #3317 2017-10-03 13:36:56 -07:00
Albert Yi
0ea7d78584 remove usage of vcr cassettes; delete unused fixtures; fix some broken unit tests 2016-12-28 15:47:28 -08:00
r888888888
fc7afd44ea refactor source pixiv test
refactor pixiv download tests
refactor upload test
refactor nico seiga test
refactor twitter tests
2016-09-28 11:25:29 -07:00
r888888888
fd38539b41 fix twitter upload tests 2016-09-12 18:02:59 -07:00
r888888888
439ea4aaff fix functional tests 2015-09-23 12:57:17 -07:00
r888888888
09f71f79d1 fix tests 2015-06-11 21:59:30 -07:00
r888888888
2ddc93723c fixes #2327 2015-06-02 19:20:09 -07:00