This repository has been archived on 2024-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
Sequenzia/app/models/BatchUpload.php

100 lines
2.8 KiB
PHP
Raw Normal View History

2013-10-27 01:06:58 +02:00
<?php
class BatchUpload extends Rails\ActiveRecord\Base
{
public $data;
2016-07-30 07:12:19 +02:00
2013-10-27 01:06:58 +02:00
/**
* Flag to know the upload is 100% finished.
*/
public $finished = false;
public function run()
{
Rails::systemExit()->register(function() {
if (!$this->finished) {
$this->active = false;
$this->data->success = false;
$this->data->error = "Couldn't finish successfuly";
$this->save();
}
});
2016-07-30 07:12:19 +02:00
2013-10-27 01:06:58 +02:00
# Ugly: set the current user ID to the one set in the batch, so history entries
# will be created as that user.
2016-07-30 07:12:19 +02:00
User::set_current_user(User::find($this->user_id));
2013-10-27 01:06:58 +02:00
$this->active = true;
$this->save();
2014-12-13 00:18:37 +01:00
$post = Post::create(['source' => $this->url, 'tags' => $this->tags, 'updater_user_id' => $this->user_id, 'updater_ip_addr' => $this->ip, 'user_id' => $this->user_id, 'ip_addr' => $this->ip, 'status' => "active", /*'is_upload' => false*/]);
2013-10-27 01:06:58 +02:00
2014-12-13 00:18:37 +01:00
if ($post->errors()->blank()) {
if (CONFIG()->dupe_check_on_upload && $post->image() && !$post->parent_id) {
$options = [ 'services' => SimilarImages::get_services("local"), 'type' => 'post', 'source' => $post ];
2013-10-27 01:06:58 +02:00
$res = SimilarImages::similar_images($options);
if (!empty($res['posts'])) {
2014-12-13 00:18:37 +01:00
$post->tags = $post->tags() . " possible_duplicate";
$post->save();
2013-10-27 01:06:58 +02:00
}
}
$this->data->success = true;
2014-12-13 00:18:37 +01:00
$this->data->post_id = $post->id;
} elseif ($post->errors()->on('md5')) {
$p = Post::where(['md5' => $post->md5])->first();
2016-07-30 07:12:19 +02:00
2013-10-27 01:06:58 +02:00
$this->data->success = false;
$this->data->error = "Post already exists";
$this->data->post_id = $p->id;
} else {
$this->data->success = false;
2014-12-13 00:18:37 +01:00
$this->data->error = $post->errors()->fullMessages(", ");
2013-10-27 01:06:58 +02:00
}
if ($this->data->success) {
$this->status = 'finished';
} else {
$this->status = 'error';
}
$this->active = false;
2016-07-30 07:12:19 +02:00
2013-10-27 01:06:58 +02:00
$this->save();
$this->finished = true;
}
2016-07-30 07:12:19 +02:00
2013-10-27 01:06:58 +02:00
protected function associations()
{
return [
'belongs_to' => [
'user'
]
];
}
protected function init()
{
$this->data = json_decode($this->data_as_json) ?: new stdClass();
}
2016-07-30 07:12:19 +02:00
2013-10-27 01:06:58 +02:00
protected function encode_data()
{
$this->data_as_json = json_encode($this->data);
}
2016-07-30 07:12:19 +02:00
2013-10-27 01:06:58 +02:00
// protected function data_setter($hoge)
// {
// $this->data_as_json = json_encode($hoge);
// }
protected function callbacks()
{
return [
'before_save' => [
'encode_data'
]
];
}
2016-07-30 07:12:19 +02:00
}