Skip to content

Commit a97a8d1

Browse files
committed
Add Drag upload
1 parent 0889d5b commit a97a8d1

File tree

8 files changed

+1251
-7
lines changed

8 files changed

+1251
-7
lines changed

main/akmarkdown.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,27 @@ public function onAfterInitialise()
111111
// Replace some text
112112
$text = str_replace('<a', '<a target="_blank"', $text);
113113

114+
echo <<<STYLE
115+
<style>
116+
img { max-width: 550px; }
117+
</style>
118+
STYLE;
119+
114120
echo $text;
115121

116122
jexit();
117123
}
124+
125+
$upload = $input->get('akmarkdown_upload');
126+
127+
if ($upload)
128+
{
129+
include_once __DIR__ . '/lib/autoload.php';
130+
131+
Akmarkdown\Uploader\ImageUploader::upload($input);
132+
133+
jexit();
134+
}
118135
}
119136

120137
/**
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
/**
3+
* Part of joomla34 project.
4+
*
5+
* @copyright Copyright (C) 2015 {ORGANIZATION}. All rights reserved.
6+
* @license GNU General Public License version 2 or later;
7+
*/
8+
9+
namespace Akmarkdown\Uploader;
10+
11+
use Akmarkdown\Web\Response;
12+
use Joomla\Registry\Registry;
13+
14+
/**
15+
* The ImageUploader class.
16+
*
17+
* @since {DEPLOY_VERSION}
18+
*/
19+
class ImageUploader
20+
{
21+
/**
22+
* upload
23+
*
24+
* @param \JInput $input
25+
*/
26+
public static function upload(\JInput $input)
27+
{
28+
try
29+
{
30+
$editorPlugin = \JPluginHelper::getPlugin('editors', 'akmarkdown');
31+
32+
if (!$editorPlugin)
33+
{
34+
throw new \Exception('Editor Akmarkdown not exists');
35+
}
36+
37+
$params = new Registry($editorPlugin->params);
38+
39+
$files = $input->files;
40+
$field = $input->get('field', 'file');
41+
$type = $input->get('type', 'post');
42+
43+
$allows = $params->get('Upload_AllowExtension', '');
44+
$allows = array_map('strtolower', array_map('trim', explode(',', $allows)));
45+
46+
$file = $files->getVar($field);
47+
$src = $file['tmp_name'];
48+
$name = $file['name'];
49+
$tmp = new \SplFileInfo(JPATH_ROOT . '/tmp/ak-upload/' . $name);
50+
51+
if (empty($file['tmp_name']))
52+
{
53+
throw new \Exception('File not upload');
54+
}
55+
56+
$ext = pathinfo($name, PATHINFO_EXTENSION);
57+
58+
if (!in_array($ext, $allows))
59+
{
60+
throw new \Exception('File extension now allowed.');
61+
}
62+
63+
// Move file to tmp
64+
if (!is_dir($tmp->getPath()))
65+
{
66+
\JFolder::create($tmp->getPath());
67+
}
68+
69+
if (is_file($tmp->getPathname()))
70+
{
71+
\JFile::delete($tmp->getPathname());
72+
}
73+
74+
\JFile::upload($src, $tmp->getPathname());
75+
76+
$src = $tmp;
77+
$dest = static::getDest($name, $params->get('Upload_S3_Subfolder', 'ak-upload'));
78+
79+
$s3 = new \S3(
80+
$params->get('Upload_S3_Key'),
81+
$params->get('Upload_S3_SecretKey')
82+
);
83+
84+
$bucket = $params->get('Upload_S3_Bucket');
85+
86+
$result = $s3::putObject(\S3::inputFile($src->getPathname(), false), $bucket, $dest, \S3::ACL_PUBLIC_READ);
87+
88+
if (is_file($tmp->getPathname()))
89+
{
90+
\JFile::delete($tmp->getPathname());
91+
}
92+
93+
if (!$result)
94+
{
95+
throw new \Exception('Upload fail.');
96+
}
97+
}
98+
catch (\Exception $e)
99+
{
100+
$response = new Response;
101+
$response->setBody(json_encode(['error' => $e->getMessage()]));
102+
$response->setMimeType('text/json');
103+
104+
$response->respond();
105+
106+
exit();
107+
}
108+
109+
$return = new \JRegistry;
110+
111+
$return['filename'] = 'https://' . $bucket . '.s3.amazonaws.com/' . $dest;
112+
$return['file'] = 'https://' . $bucket . '.s3.amazonaws.com/' . $dest;
113+
114+
$response = new Response;
115+
$response->setBody((string) $return);
116+
$response->setMimeType('text/json');
117+
118+
$response->respond();
119+
}
120+
121+
/**
122+
* getDest
123+
*
124+
* @param string $name
125+
* @param string $subfolder
126+
*
127+
* @return string
128+
*/
129+
protected static function getDest($name, $subfolder = 'ak-upload')
130+
{
131+
$user = \JFactory::getUser();
132+
133+
$user->username = $user->username ? : 'guest';
134+
135+
$date = new \JDate('now');
136+
137+
$year = $date->year;
138+
$month = $date->month;
139+
$day = $date->day;
140+
141+
$ext = pathinfo($name, PATHINFO_EXTENSION);
142+
143+
return sprintf('%s/%s/%s/%s/%s/%s.%s', $subfolder, $user->username, $year, $month, $day, uniqid(), $ext);
144+
}
145+
}

0 commit comments

Comments
 (0)