| 25380 |
amit.gupta |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import com.google.gson.Gson;
|
| 30080 |
amit.gupta |
4 |
import com.jcraft.jsch.*;
|
| 25380 |
amit.gupta |
5 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
6 |
import com.spice.profitmandi.common.solr.SolrService;
|
|
|
7 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 28264 |
tejbeer |
8 |
import com.spice.profitmandi.dao.entity.dtr.DocumentUrl;
|
| 25380 |
amit.gupta |
9 |
import com.spice.profitmandi.dao.model.ContentPojo;
|
|
|
10 |
import com.spice.profitmandi.dao.model.MediaPojo;
|
|
|
11 |
import com.spice.profitmandi.dao.model.Specification;
|
|
|
12 |
import com.spice.profitmandi.dao.model.SpecificationGroup;
|
| 31330 |
tejbeer |
13 |
import com.spice.profitmandi.dao.repository.catalog.FocusedModelRepository;
|
| 25380 |
amit.gupta |
14 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| 28264 |
tejbeer |
15 |
import com.spice.profitmandi.dao.repository.dtr.DocumentUrlRepository;
|
| 25380 |
amit.gupta |
16 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
| 32952 |
amit.gupta |
17 |
import com.spice.profitmandi.service.catalog.ItemLoaderService;
|
| 25380 |
amit.gupta |
18 |
import com.spice.profitmandi.web.model.EntityMediaPojo;
|
|
|
19 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
| 30080 |
amit.gupta |
20 |
import org.apache.commons.csv.CSVFormat;
|
|
|
21 |
import org.apache.commons.csv.CSVParser;
|
|
|
22 |
import org.apache.commons.csv.CSVRecord;
|
|
|
23 |
import org.apache.commons.lang3.StringUtils;
|
|
|
24 |
import org.apache.logging.log4j.LogManager;
|
|
|
25 |
import org.apache.logging.log4j.Logger;
|
| 31330 |
tejbeer |
26 |
import org.json.JSONArray;
|
|
|
27 |
import org.json.JSONObject;
|
| 30080 |
amit.gupta |
28 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
29 |
import org.springframework.stereotype.Controller;
|
|
|
30 |
import org.springframework.ui.Model;
|
|
|
31 |
import org.springframework.web.bind.annotation.*;
|
|
|
32 |
import org.springframework.web.multipart.MultipartFile;
|
| 25380 |
amit.gupta |
33 |
|
| 30080 |
amit.gupta |
34 |
import javax.servlet.http.HttpServletRequest;
|
|
|
35 |
import javax.transaction.Transactional;
|
|
|
36 |
import javax.xml.bind.DatatypeConverter;
|
|
|
37 |
import java.io.*;
|
|
|
38 |
import java.sql.Timestamp;
|
|
|
39 |
import java.time.LocalDate;
|
|
|
40 |
import java.time.LocalDateTime;
|
|
|
41 |
import java.time.ZoneOffset;
|
|
|
42 |
import java.util.*;
|
|
|
43 |
import java.util.stream.Collectors;
|
|
|
44 |
|
| 25380 |
amit.gupta |
45 |
@Transactional(rollbackOn = Throwable.class)
|
|
|
46 |
@Controller
|
|
|
47 |
public class ContentController {
|
|
|
48 |
@Autowired
|
|
|
49 |
MVCResponseSender mvcResponseSender;
|
|
|
50 |
|
|
|
51 |
@Autowired
|
|
|
52 |
Mongo mongoClient;
|
|
|
53 |
|
|
|
54 |
@Autowired
|
|
|
55 |
SolrService solrService;
|
|
|
56 |
|
|
|
57 |
@Autowired
|
|
|
58 |
ItemRepository itemRepository;
|
|
|
59 |
|
| 28264 |
tejbeer |
60 |
@Autowired
|
|
|
61 |
DocumentUrlRepository documentUrlRepository;
|
|
|
62 |
|
| 31330 |
tejbeer |
63 |
@Autowired
|
|
|
64 |
FocusedModelRepository focusedModelRepository;
|
| 25380 |
amit.gupta |
65 |
private Gson gson = new Gson();
|
|
|
66 |
|
|
|
67 |
public static final int Entity_Id = 0;
|
|
|
68 |
public static final int Title = 1;
|
|
|
69 |
public static final int KeySpec1 = 2;
|
|
|
70 |
public static final int KeySpec2 = 3;
|
|
|
71 |
public static final int KeySpec3 = 4;
|
|
|
72 |
public static final int KeySpec4 = 5;
|
|
|
73 |
public static final int Warranty = 6;
|
|
|
74 |
public static final int Package_Contents = 7;
|
| 28264 |
tejbeer |
75 |
private static final String IMAGE_REMOTE_DIR = "/var/www/dtrdashboard/uploads/campaigns/";
|
|
|
76 |
private static final String IMAGE_STATIC_SERVER_URL = "https://images.smartdukaan.com/uploads/campaigns";
|
| 25380 |
amit.gupta |
77 |
|
| 29900 |
amit.gupta |
78 |
private static final String REMOTE_DIR = "/var/www/static.saholic.com/images/media/";
|
| 31330 |
tejbeer |
79 |
// private static final String REMOTE_DIR = "/tmp";
|
| 25986 |
amit.gupta |
80 |
private static final String STATIC_SERVER_URL = "https://static%d.smartdukaan.com/images/media/";
|
| 25380 |
amit.gupta |
81 |
|
|
|
82 |
private static final String THUMBNAIL = "thumbnail";
|
|
|
83 |
private static final String ICON = "icon";
|
|
|
84 |
private static final String DEFAULT = "default";
|
|
|
85 |
private static final String NONE = "";
|
| 25409 |
amit.gupta |
86 |
|
| 25400 |
amit.gupta |
87 |
private static final Logger LOGGER = LogManager.getLogger(ContentController.class);
|
| 25380 |
amit.gupta |
88 |
|
|
|
89 |
private String getFileName(int entityId, String description, String extension) {
|
|
|
90 |
List<Item> items = itemRepository.selectAllByCatalogItemId(entityId);
|
| 25409 |
amit.gupta |
91 |
String imageString = getMediaPrefix(items.get(0).getItemDescriptionNoColor() + " " + description) + "-"
|
| 25380 |
amit.gupta |
92 |
+ LocalDateTime.now().toEpochSecond(ZoneOffset.ofHoursMinutes(5, 30));
|
| 25413 |
amit.gupta |
93 |
return imageString + "." + extension;
|
| 25380 |
amit.gupta |
94 |
}
|
|
|
95 |
|
|
|
96 |
@PostMapping(value = "/content/upload")
|
|
|
97 |
public String uploadContent(HttpServletRequest request, @RequestPart("file") MultipartFile file, Model model)
|
|
|
98 |
throws Exception {
|
|
|
99 |
List<ContentPojo> contentPojos = readFile(file);
|
|
|
100 |
for (ContentPojo contentPojo : contentPojos) {
|
|
|
101 |
mongoClient.persistEntity(contentPojo);
|
|
|
102 |
}
|
| 31238 |
amit.gupta |
103 |
model.addAttribute("response1", mvcResponseSender.createResponseString(true));
|
| 25380 |
amit.gupta |
104 |
return "response";
|
|
|
105 |
}
|
|
|
106 |
|
| 32952 |
amit.gupta |
107 |
@Autowired
|
|
|
108 |
ItemLoaderService itemLoaderService;
|
|
|
109 |
@PostMapping(value = "/catalog/upload")
|
|
|
110 |
public String uploadCatalog(HttpServletRequest request, @RequestPart("file") MultipartFile file, Model model)
|
|
|
111 |
throws Exception {
|
|
|
112 |
itemLoaderService.parse(file);
|
|
|
113 |
model.addAttribute("response1", mvcResponseSender.createResponseString(true));
|
|
|
114 |
return "response";
|
|
|
115 |
}
|
|
|
116 |
|
| 25380 |
amit.gupta |
117 |
@PostMapping(value = "/content/media/upload")
|
|
|
118 |
public String uploadMediaContent(HttpServletRequest request, @RequestBody EntityMediaPojo entityMediaPojo,
|
|
|
119 |
Model model) throws Exception {
|
|
|
120 |
ContentPojo contentPojo = mongoClient.getEntityById(entityMediaPojo.getEntityId());
|
|
|
121 |
Map<String, InputStream> fileStreamsMap = getStreamFileMap(contentPojo, entityMediaPojo);
|
| 25400 |
amit.gupta |
122 |
LOGGER.info("fileStreamsMap {}, " + fileStreamsMap.keySet());
|
| 29900 |
amit.gupta |
123 |
uploadContentFiles(fileStreamsMap, entityMediaPojo.getEntityId());
|
| 25380 |
amit.gupta |
124 |
mongoClient.persistEntity(contentPojo);
|
| 31238 |
amit.gupta |
125 |
model.addAttribute("response1", mvcResponseSender.createResponseString(true));
|
| 25380 |
amit.gupta |
126 |
return "response";
|
|
|
127 |
}
|
|
|
128 |
|
| 28264 |
tejbeer |
129 |
@PostMapping(value = "/image/media/upload")
|
|
|
130 |
public String uploadImageMediaContent(HttpServletRequest request, @RequestBody MediaPojo mediaPojo, Model model)
|
|
|
131 |
throws Exception {
|
|
|
132 |
|
|
|
133 |
LOGGER.info("mediaPojo" + mediaPojo);
|
|
|
134 |
Map<String, InputStream> fileStreamsMap = new HashMap<>();
|
|
|
135 |
String extension;
|
|
|
136 |
String base64String = mediaPojo.getImageData();
|
|
|
137 |
String[] strings = base64String.split(",");
|
|
|
138 |
switch (strings[0]) {// check image's extension
|
|
|
139 |
case "data:image/jpeg;base64":
|
|
|
140 |
extension = "jpeg";
|
|
|
141 |
break;
|
|
|
142 |
case "data:image/png;base64":
|
|
|
143 |
extension = "png";
|
|
|
144 |
break;
|
| 32304 |
amit.gupta |
145 |
case "data:image/webp;base64":
|
| 32305 |
amit.gupta |
146 |
extension = "webp";
|
| 32304 |
amit.gupta |
147 |
break;
|
| 28264 |
tejbeer |
148 |
default:// should write cases for more images types
|
| 32304 |
amit.gupta |
149 |
extension = "jpg";
|
| 28264 |
tejbeer |
150 |
break;
|
|
|
151 |
}
|
|
|
152 |
LOGGER.info("After switch statement = {}", extension);
|
|
|
153 |
// convert base64 string to binary data
|
|
|
154 |
byte[] data = DatatypeConverter.parseBase64Binary(strings[1]);
|
|
|
155 |
Timestamp tm = new Timestamp(System.currentTimeMillis());
|
| 28320 |
tejbeer |
156 |
String urlTitle = mediaPojo.getTitle().toLowerCase();
|
|
|
157 |
String fileName = urlTitle.replace(' ', '-') + tm.getTime() + "." + extension;
|
| 28264 |
tejbeer |
158 |
LOGGER.info("After switch statement Filename = {}", fileName);
|
|
|
159 |
mediaPojo.setImageData(null);
|
|
|
160 |
mediaPojo.setUrl(IMAGE_STATIC_SERVER_URL + "/" + "image" + LocalDate.now() + "/" + fileName);
|
|
|
161 |
fileStreamsMap.put(fileName, new ByteArrayInputStream(data));
|
|
|
162 |
LOGGER.info("fileStreamsMap" + fileStreamsMap);
|
|
|
163 |
|
|
|
164 |
uploadFile(fileStreamsMap);
|
|
|
165 |
|
|
|
166 |
DocumentUrl du = new DocumentUrl();
|
|
|
167 |
du.setTitle(mediaPojo.getTitle());
|
|
|
168 |
du.setUrl(mediaPojo.getUrl());
|
|
|
169 |
du.setCreatedTimestamp(LocalDateTime.now());
|
|
|
170 |
documentUrlRepository.persist(du);
|
|
|
171 |
|
|
|
172 |
model.addAttribute("documents", du);
|
|
|
173 |
return "image-url";
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
private void fileUpload(ChannelSftp channelSftp, Map<String, InputStream> streamsFileMap, String destinationPath)
|
|
|
177 |
throws SftpException, FileNotFoundException {
|
|
|
178 |
|
|
|
179 |
channelSftp.cd(destinationPath);
|
|
|
180 |
String folderName = "image" + LocalDate.now();
|
|
|
181 |
|
|
|
182 |
channelSftp.cd(destinationPath);
|
|
|
183 |
SftpATTRS attrs = null;
|
|
|
184 |
|
|
|
185 |
// check if the directory is already existing
|
|
|
186 |
try {
|
|
|
187 |
attrs = channelSftp.stat(folderName);
|
|
|
188 |
} catch (Exception e) {
|
|
|
189 |
System.out.println(destinationPath + "/" + "image" + LocalDate.now() + " not found");
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
// else create a directory
|
|
|
193 |
if (attrs == null) {
|
|
|
194 |
channelSftp.mkdir(folderName);
|
|
|
195 |
channelSftp.chmod(0755, ".");
|
|
|
196 |
}
|
|
|
197 |
channelSftp.cd(folderName);
|
|
|
198 |
|
|
|
199 |
for (Map.Entry<String, InputStream> streamsFileEntry : streamsFileMap.entrySet()) {
|
|
|
200 |
channelSftp.put(streamsFileEntry.getValue(), streamsFileEntry.getKey(), ChannelSftp.OVERWRITE);
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
@GetMapping(value = "/search/image/media")
|
|
|
206 |
public String getImage(HttpServletRequest request, Model model, @RequestParam String title) throws Exception {
|
|
|
207 |
List<DocumentUrl> documents = documentUrlRepository.selectBySearch(title);
|
|
|
208 |
|
|
|
209 |
model.addAttribute("documents", documents);
|
|
|
210 |
return "image-url";
|
|
|
211 |
|
|
|
212 |
}
|
|
|
213 |
|
| 25380 |
amit.gupta |
214 |
@GetMapping(value = "/content/media")
|
|
|
215 |
public String getMediaContent(HttpServletRequest request, Model model, @RequestParam int entityId)
|
|
|
216 |
throws Exception {
|
|
|
217 |
ContentPojo contentPojo = mongoClient.getEntityById(entityId);
|
| 25409 |
amit.gupta |
218 |
if (contentPojo == null) {
|
| 25380 |
amit.gupta |
219 |
throw new Exception("Please add content first");
|
|
|
220 |
}
|
|
|
221 |
EntityMediaPojo empojo = getEntityMediaPojo(contentPojo);
|
| 31238 |
amit.gupta |
222 |
model.addAttribute("response1", mvcResponseSender.createResponseString(empojo));
|
| 25380 |
amit.gupta |
223 |
return "response";
|
|
|
224 |
}
|
| 28264 |
tejbeer |
225 |
|
| 27185 |
amit.gupta |
226 |
@GetMapping(value = "/catalog-item")
|
| 28264 |
tejbeer |
227 |
public String catalogItem(HttpServletRequest request, Model model) throws Exception {
|
| 27185 |
amit.gupta |
228 |
return "catalog-item";
|
|
|
229 |
}
|
| 25380 |
amit.gupta |
230 |
|
|
|
231 |
private EntityMediaPojo getEntityMediaPojo(ContentPojo contentPojo) {
|
|
|
232 |
EntityMediaPojo ep = new EntityMediaPojo();
|
|
|
233 |
int defaultIndex = 0;
|
| 25409 |
amit.gupta |
234 |
if (contentPojo.getImages() == null) {
|
| 25380 |
amit.gupta |
235 |
ep.setMediaPojos(new ArrayList<>());
|
|
|
236 |
} else {
|
|
|
237 |
ep.setMediaPojos(contentPojo.getImages());
|
|
|
238 |
for (MediaPojo mediaPojo : contentPojo.getImages()) {
|
| 25409 |
amit.gupta |
239 |
if (mediaPojo.getUrl() == null)
|
|
|
240 |
continue;
|
|
|
241 |
|
| 25380 |
amit.gupta |
242 |
if (!mediaPojo.getUrl().equals(contentPojo.getDefaultImageUrl())) {
|
|
|
243 |
defaultIndex++;
|
|
|
244 |
}
|
|
|
245 |
}
|
|
|
246 |
}
|
|
|
247 |
ep.setDefaultImageIndex(defaultIndex);
|
|
|
248 |
return ep;
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
@GetMapping(value = "/entity")
|
| 28264 |
tejbeer |
252 |
public String searchEntity(HttpServletRequest request, @RequestParam(defaultValue = "null") String query,
|
| 31330 |
tejbeer |
253 |
@RequestParam(defaultValue = "0") int categoryId, @RequestParam(defaultValue = "") List<String> brands,
|
|
|
254 |
@RequestParam(defaultValue = "30") int limit, @RequestParam(defaultValue = "true") boolean activeOnly,
|
|
|
255 |
Model model) throws Exception {
|
|
|
256 |
model.addAttribute("response1", solrService.getContent(query, categoryId > 0 ? Arrays.asList(categoryId) : null,
|
| 31711 |
amit.gupta |
257 |
brands, limit, false));
|
| 25380 |
amit.gupta |
258 |
return "response";
|
|
|
259 |
}
|
|
|
260 |
|
| 31330 |
tejbeer |
261 |
@GetMapping(value = "/focusedEntity")
|
|
|
262 |
public String focusedEntity(HttpServletRequest request, @RequestParam(defaultValue = "null") String query,
|
|
|
263 |
@RequestParam(defaultValue = "0") int categoryId, @RequestParam(defaultValue = "") List<String> brands,
|
|
|
264 |
@RequestParam(defaultValue = "30") int limit, @RequestParam(defaultValue = "true") boolean activeOnly,
|
|
|
265 |
Model model) throws Exception {
|
|
|
266 |
|
|
|
267 |
List<Integer> catalogIds = focusedModelRepository.selectAll().stream().map(x -> x.getCatalogId())
|
|
|
268 |
.collect(Collectors.toList());
|
|
|
269 |
JSONArray docA = solrService.getContentDocs(query, categoryId > 0 ? Arrays.asList(categoryId) : null, brands,
|
|
|
270 |
limit, activeOnly);
|
|
|
271 |
|
|
|
272 |
Iterator<Object> jsonIterator = docA.iterator();
|
|
|
273 |
while (jsonIterator.hasNext()) {
|
|
|
274 |
JSONObject i = (JSONObject) jsonIterator.next();
|
|
|
275 |
|
|
|
276 |
if (!catalogIds.contains(i.get("catalogId_i"))) {
|
|
|
277 |
jsonIterator.remove();
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
model.addAttribute("response1", docA.toString());
|
|
|
283 |
|
|
|
284 |
return "response";
|
|
|
285 |
}
|
|
|
286 |
|
| 25380 |
amit.gupta |
287 |
@GetMapping(value = "/content/index")
|
|
|
288 |
public String index(HttpServletRequest request, Model model) throws Exception {
|
|
|
289 |
return "content";
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
private List<ContentPojo> readFile(MultipartFile file) throws Exception {
|
|
|
293 |
CSVParser parser = new CSVParser(new InputStreamReader(file.getInputStream()), CSVFormat.DEFAULT);
|
|
|
294 |
List<CSVRecord> records = parser.getRecords();
|
|
|
295 |
if (records.size() < 2) {
|
|
|
296 |
parser.close();
|
|
|
297 |
throw new ProfitMandiBusinessException("Uploaded File", "", "No records Found");
|
|
|
298 |
}
|
|
|
299 |
// Remove header
|
|
|
300 |
records.remove(0);
|
|
|
301 |
List<ContentPojo> returnList = new ArrayList<ContentPojo>();
|
|
|
302 |
for (CSVRecord record : records) {
|
| 25430 |
amit.gupta |
303 |
ContentPojo cp = null;
|
|
|
304 |
Long entityId = Long.parseLong(record.get(Entity_Id));
|
| 25380 |
amit.gupta |
305 |
try {
|
| 25430 |
amit.gupta |
306 |
cp = mongoClient.getEntityById(entityId);
|
|
|
307 |
} catch (Exception e) {
|
|
|
308 |
}
|
|
|
309 |
try {
|
|
|
310 |
if (cp == null) {
|
|
|
311 |
cp = new ContentPojo(entityId);
|
|
|
312 |
}
|
| 25380 |
amit.gupta |
313 |
cp.setWarranty(record.get(Warranty));
|
|
|
314 |
cp.setKeySpecs(Arrays
|
|
|
315 |
.asList(record.get(KeySpec1), record.get(KeySpec2), record.get(KeySpec3), record.get(KeySpec4))
|
| 25429 |
amit.gupta |
316 |
.stream().filter(x -> x != null && !x.equals("")).collect(Collectors.toList()));
|
| 25380 |
amit.gupta |
317 |
cp.setPackageContents(Arrays.asList(record.get(Package_Contents).split(",")));
|
|
|
318 |
cp.setDetailedSpecs(getDetailedSpecs(record));
|
|
|
319 |
returnList.add(cp);
|
|
|
320 |
} catch (Exception e) {
|
|
|
321 |
continue;
|
|
|
322 |
}
|
|
|
323 |
}
|
|
|
324 |
parser.close();
|
|
|
325 |
return returnList;
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
private List<SpecificationGroup> getDetailedSpecs(CSVRecord record) throws Exception {
|
|
|
329 |
List<SpecificationGroup> specificationGroups = new ArrayList<>();
|
|
|
330 |
int currentIndex = 8;
|
|
|
331 |
while (StringUtils.isNotEmpty(record.get(currentIndex))) {
|
|
|
332 |
int start = currentIndex;
|
|
|
333 |
List<Specification> specifications = new ArrayList<>();
|
|
|
334 |
int begin = 0;
|
|
|
335 |
while (begin < 5) {
|
|
|
336 |
int specKeyIndex = (begin * 2) + 1;
|
|
|
337 |
int specValueIndex = (begin * 2) + 2;
|
|
|
338 |
|
|
|
339 |
if (StringUtils.isNotEmpty(record.get(currentIndex + specKeyIndex))
|
|
|
340 |
&& StringUtils.isNotEmpty(record.get(currentIndex + specValueIndex))) {
|
|
|
341 |
Specification specification = new Specification(record.get(currentIndex + specKeyIndex),
|
|
|
342 |
Arrays.asList(record.get(currentIndex + specValueIndex)));
|
|
|
343 |
specifications.add(specification);
|
|
|
344 |
}
|
|
|
345 |
begin++;
|
|
|
346 |
}
|
|
|
347 |
SpecificationGroup specificationGroup = new SpecificationGroup(record.get(start), specifications);
|
|
|
348 |
specificationGroups.add(specificationGroup);
|
| 25392 |
amit.gupta |
349 |
currentIndex += 11;
|
| 25380 |
amit.gupta |
350 |
}
|
|
|
351 |
|
|
|
352 |
return specificationGroups;
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
private ChannelSftp setupJsch() throws JSchException {
|
|
|
356 |
JSch jsch = new JSch();
|
| 28266 |
tejbeer |
357 |
Session jschSession = jsch.getSession("root", "192.168.179.131");
|
| 31330 |
tejbeer |
358 |
// Session jschSession = jsch.getSession("root", "173.255.254.24");
|
|
|
359 |
LOGGER.info("getClass().getResource(\"id_rsa\") {}",
|
|
|
360 |
getClass().getClassLoader().getResource("id_rsa").getPath());
|
| 29881 |
amit.gupta |
361 |
jsch.addIdentity(getClass().getClassLoader().getResource("id_rsa").getPath());
|
| 31330 |
tejbeer |
362 |
// jschSession.setPassword("spic@2015static0");
|
| 25396 |
amit.gupta |
363 |
jschSession.setConfig("StrictHostKeyChecking", "no");
|
| 25380 |
amit.gupta |
364 |
jschSession.connect();
|
|
|
365 |
return (ChannelSftp) jschSession.openChannel("sftp");
|
|
|
366 |
}
|
|
|
367 |
|
| 29900 |
amit.gupta |
368 |
private void uploadFile(Map<String, InputStream> fileStreamsMap) throws Exception {
|
| 25380 |
amit.gupta |
369 |
ChannelSftp channelSftp = setupJsch();
|
|
|
370 |
channelSftp.connect();
|
| 29900 |
amit.gupta |
371 |
this.fileUpload(channelSftp, fileStreamsMap, IMAGE_REMOTE_DIR + "");
|
|
|
372 |
channelSftp.exit();
|
|
|
373 |
}
|
|
|
374 |
|
|
|
375 |
private void uploadContentFiles(Map<String, InputStream> fileStreamsMap, int entityId) throws Exception {
|
|
|
376 |
ChannelSftp channelSftp = setupJsch();
|
|
|
377 |
channelSftp.connect();
|
| 25380 |
amit.gupta |
378 |
this.folderUpload(channelSftp, fileStreamsMap, REMOTE_DIR, entityId + "");
|
|
|
379 |
channelSftp.exit();
|
|
|
380 |
}
|
|
|
381 |
|
|
|
382 |
private void recursiveFolderUpload(ChannelSftp channelSftp, String sourcePath, String destinationPath)
|
|
|
383 |
throws SftpException, FileNotFoundException {
|
|
|
384 |
|
|
|
385 |
File sourceFile = new File(sourcePath);
|
|
|
386 |
if (sourceFile.isFile()) {
|
|
|
387 |
|
|
|
388 |
// copy if it is a file
|
|
|
389 |
channelSftp.cd(destinationPath);
|
|
|
390 |
if (!sourceFile.getName().startsWith("."))
|
|
|
391 |
channelSftp.put(new FileInputStream(sourceFile), sourceFile.getName(), ChannelSftp.OVERWRITE);
|
|
|
392 |
|
|
|
393 |
} else {
|
|
|
394 |
|
|
|
395 |
System.out.println("inside else " + sourceFile.getName());
|
|
|
396 |
File[] files = sourceFile.listFiles();
|
|
|
397 |
|
|
|
398 |
if (files != null && !sourceFile.getName().startsWith(".")) {
|
|
|
399 |
|
|
|
400 |
channelSftp.cd(destinationPath);
|
|
|
401 |
SftpATTRS attrs = null;
|
|
|
402 |
|
|
|
403 |
// check if the directory is already existing
|
|
|
404 |
try {
|
|
|
405 |
attrs = channelSftp.stat(destinationPath + "/" + sourceFile.getName());
|
|
|
406 |
} catch (Exception e) {
|
|
|
407 |
System.out.println(destinationPath + "/" + sourceFile.getName() + " not found");
|
|
|
408 |
}
|
|
|
409 |
|
|
|
410 |
// else create a directory
|
|
|
411 |
if (attrs != null) {
|
|
|
412 |
System.out.println("Directory exists IsDir=" + attrs.isDir());
|
|
|
413 |
} else {
|
|
|
414 |
System.out.println("Creating dir " + sourceFile.getName());
|
|
|
415 |
channelSftp.mkdir(sourceFile.getName());
|
|
|
416 |
}
|
|
|
417 |
|
|
|
418 |
for (File f : files) {
|
|
|
419 |
recursiveFolderUpload(channelSftp, f.getAbsolutePath(),
|
|
|
420 |
destinationPath + "/" + sourceFile.getName());
|
|
|
421 |
}
|
|
|
422 |
|
|
|
423 |
}
|
|
|
424 |
}
|
|
|
425 |
|
|
|
426 |
}
|
|
|
427 |
|
|
|
428 |
private void folderUpload(ChannelSftp channelSftp, Map<String, InputStream> streamsFileMap, String destinationPath,
|
|
|
429 |
String folderName) throws SftpException, FileNotFoundException {
|
|
|
430 |
|
|
|
431 |
channelSftp.cd(destinationPath);
|
|
|
432 |
SftpATTRS attrs = null;
|
|
|
433 |
|
|
|
434 |
// check if the directory is already existing
|
|
|
435 |
try {
|
|
|
436 |
attrs = channelSftp.stat(folderName);
|
|
|
437 |
} catch (Exception e) {
|
|
|
438 |
System.out.println(destinationPath + "/" + folderName + " not found");
|
|
|
439 |
}
|
|
|
440 |
|
|
|
441 |
// else create a directory
|
| 25413 |
amit.gupta |
442 |
if (attrs == null) {
|
|
|
443 |
channelSftp.mkdir(folderName);
|
|
|
444 |
channelSftp.chmod(0755, ".");
|
| 25380 |
amit.gupta |
445 |
}
|
|
|
446 |
channelSftp.cd(folderName);
|
| 25413 |
amit.gupta |
447 |
channelSftp.rm("*");
|
| 25380 |
amit.gupta |
448 |
|
|
|
449 |
for (Map.Entry<String, InputStream> streamsFileEntry : streamsFileMap.entrySet()) {
|
|
|
450 |
channelSftp.put(streamsFileEntry.getValue(), streamsFileEntry.getKey(), ChannelSftp.OVERWRITE);
|
|
|
451 |
}
|
|
|
452 |
|
|
|
453 |
}
|
|
|
454 |
|
|
|
455 |
private Map<String, InputStream> getStreamFileMap(ContentPojo contentPojo, EntityMediaPojo entityMediaPojo) {
|
|
|
456 |
Map<String, InputStream> map = new HashMap<>();
|
| 25401 |
amit.gupta |
457 |
LOGGER.info("entityMediaPojo.getMediaPojos() -[{}]", entityMediaPojo.getMediaPojos());
|
| 25404 |
amit.gupta |
458 |
for (int i = 0; i < entityMediaPojo.getMediaPojos().size(); i++) {
|
| 25380 |
amit.gupta |
459 |
MediaPojo mediaPojo = entityMediaPojo.getMediaPojos().get(i);
|
|
|
460 |
String extension;
|
|
|
461 |
String base64String = mediaPojo.getImageData();
|
|
|
462 |
String[] strings = base64String.split(",");
|
|
|
463 |
switch (strings[0]) {// check image's extension
|
|
|
464 |
case "data:image/jpeg;base64":
|
|
|
465 |
extension = "jpeg";
|
|
|
466 |
break;
|
|
|
467 |
case "data:image/png;base64":
|
|
|
468 |
extension = "png";
|
|
|
469 |
break;
|
|
|
470 |
default:// should write cases for more images types
|
|
|
471 |
extension = "jpg";
|
|
|
472 |
break;
|
|
|
473 |
}
|
| 25402 |
amit.gupta |
474 |
LOGGER.info("After switch statement = {}", extension);
|
| 25380 |
amit.gupta |
475 |
// convert base64 string to binary data
|
|
|
476 |
byte[] data = DatatypeConverter.parseBase64Binary(strings[1]);
|
|
|
477 |
String fileName = getFileName(entityMediaPojo.getEntityId(), mediaPojo.getTitle(), extension);
|
| 25402 |
amit.gupta |
478 |
LOGGER.info("After switch statement Filename = {}", fileName);
|
| 25380 |
amit.gupta |
479 |
mediaPojo.setImageData(null);
|
| 25430 |
amit.gupta |
480 |
String staticServerUrl = String.format(STATIC_SERVER_URL, entityMediaPojo.getEntityId() % 3);
|
| 25414 |
amit.gupta |
481 |
mediaPojo.setUrl(staticServerUrl + entityMediaPojo.getEntityId() + "/" + fileName);
|
| 25380 |
amit.gupta |
482 |
map.put(fileName, new ByteArrayInputStream(data));
|
|
|
483 |
if (i == entityMediaPojo.getDefaultImageIndex()) {
|
|
|
484 |
String defaultFileName = getFileName(entityMediaPojo.getEntityId(), DEFAULT, extension);
|
|
|
485 |
map.put(defaultFileName, new ByteArrayInputStream(data));
|
| 25414 |
amit.gupta |
486 |
contentPojo.setDefaultImageUrl(staticServerUrl + entityMediaPojo.getEntityId() + "/" + defaultFileName);
|
| 25380 |
amit.gupta |
487 |
}
|
|
|
488 |
}
|
|
|
489 |
contentPojo.setImages(entityMediaPojo.getMediaPojos());
|
|
|
490 |
|
|
|
491 |
return map;
|
|
|
492 |
}
|
|
|
493 |
|
| 25409 |
amit.gupta |
494 |
private String getMediaPrefix(String productName) {
|
|
|
495 |
String mediaPrefix = productName.toLowerCase().replace(' ', '-');
|
|
|
496 |
mediaPrefix = mediaPrefix.replaceAll("/", "-");
|
|
|
497 |
mediaPrefix = mediaPrefix.replaceAll("-+", "-");
|
|
|
498 |
return mediaPrefix;
|
|
|
499 |
}
|
|
|
500 |
|
| 28264 |
tejbeer |
501 |
@GetMapping(value = "/urlGeneration")
|
|
|
502 |
public String urlGeneration(HttpServletRequest request, Model model) throws Exception {
|
|
|
503 |
return "url-generation";
|
|
|
504 |
}
|
| 25380 |
amit.gupta |
505 |
}
|