Subversion Repositories SmartDukaan

Rev

Rev 35102 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 35102 Rev 35204
Line 24... Line 24...
24
import javax.mail.Multipart;
24
import javax.mail.Multipart;
25
import javax.mail.internet.InternetAddress;
25
import javax.mail.internet.InternetAddress;
26
import javax.mail.internet.MimeBodyPart;
26
import javax.mail.internet.MimeBodyPart;
27
import javax.mail.internet.MimeMessage;
27
import javax.mail.internet.MimeMessage;
28
import javax.mail.internet.MimeMultipart;
28
import javax.mail.internet.MimeMultipart;
29
import java.io.File;
29
import java.io.*;
30
import java.io.IOException;
-
 
31
import java.io.Serializable;
-
 
32
import java.time.*;
30
import java.time.*;
33
import java.util.*;
31
import java.util.*;
34
import java.util.concurrent.ConcurrentHashMap;
32
import java.util.concurrent.ConcurrentHashMap;
35
import java.util.function.Function;
33
import java.util.function.Function;
36
import java.util.function.Predicate;
34
import java.util.function.Predicate;
Line 246... Line 244...
246
        if (bcc != null) {
244
        if (bcc != null) {
247
            helper.setBcc(bcc);
245
            helper.setBcc(bcc);
248
        }
246
        }
249
        helper.setTo(emailTo);
247
        helper.setTo(emailTo);
250
        for (Attachment attachment : attachments) {
248
        for (Attachment attachment : attachments) {
-
 
249
            //saveInputStreamSourceToHome(attachment.getInputStreamSource(), attachment.getFileName());
251
            helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
250
            helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
252
        }
251
        }
253
        InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
252
        InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
254
        helper.setFrom(senderAddress);
253
        helper.setFrom(senderAddress);
255
        mailSender.send(message);
254
        mailSender.send(message);
Line 461... Line 460...
461
            helper.setCc(cc);
460
            helper.setCc(cc);
462
        }
461
        }
463
        helper.setTo(emailTo);
462
        helper.setTo(emailTo);
464
        for (Attachment attachment : attachments) {
463
        for (Attachment attachment : attachments) {
465
            helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
464
            helper.addAttachment(attachment.getFileName(), attachment.getInputStreamSource());
-
 
465
            //saveInputStreamSourceToHome(attachment.getInputStreamSource(), attachment.getFileName());
466
        }
466
        }
467
        InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
467
        InternetAddress senderAddress = new InternetAddress("noreply@smartdukaan.com", "SmartDukaan Care");
468
        helper.setFrom(senderAddress);
468
        helper.setFrom(senderAddress);
469
        mailSender.send(message);
469
        mailSender.send(message);
470
    }
470
    }
Line 504... Line 504...
504
        return Instant.ofEpochMilli(dateToConvert.getTime())
504
        return Instant.ofEpochMilli(dateToConvert.getTime())
505
                .atZone(ZoneId.systemDefault())
505
                .atZone(ZoneId.systemDefault())
506
                .toLocalDateTime();
506
                .toLocalDateTime();
507
    }
507
    }
508
 
508
 
-
 
509
    /**
-
 
510
     * Saves the given InputStream to the specified File.
-
 
511
     * Automatically creates parent directories if they don't exist.
-
 
512
     *
-
 
513
     * @param inputStream the source InputStream
-
 
514
     * @param file        the destination file
-
 
515
     * @throws IOException if an I/O error occurs
-
 
516
     */
-
 
517
    public static void saveStreamToFile(InputStream inputStream, File file) throws IOException {
-
 
518
        // Ensure parent directories exist
-
 
519
        if (file.getParentFile() != null && !file.getParentFile().exists()) {
-
 
520
            file.getParentFile().mkdirs();
-
 
521
        }
-
 
522
 
-
 
523
        try (InputStream in = inputStream;
-
 
524
             OutputStream out = new FileOutputStream(file)) {
-
 
525
            byte[] buffer = new byte[8192];  // 8 KB buffer
-
 
526
            int bytesRead;
-
 
527
            while ((bytesRead = in.read(buffer)) != -1) {
-
 
528
                out.write(buffer, bytesRead == 0 ? 0 : 0, bytesRead);
-
 
529
            }
-
 
530
        }
-
 
531
    }
-
 
532
 
-
 
533
    /**
-
 
534
     * Saves a Spring InputStreamSource to the specified file.
-
 
535
     * Works on all OS (Windows, macOS, Linux) and Java 8+.
-
 
536
     */
-
 
537
    /**
-
 
538
     * Saves a Spring InputStreamSource to a file inside the user's home directory.
-
 
539
     * Works on Java 8+ and all operating systems.
-
 
540
     *
-
 
541
     * @param source    the InputStreamSource (e.g., MultipartFile, ByteArrayResource)
-
 
542
     * @param fileName  the file name (without path)
-
 
543
     * @return the created File reference
-
 
544
     * @throws IOException if an error occurs during write
-
 
545
     */
-
 
546
    public static File saveInputStreamSourceToHome(InputStreamSource source, String fileName) throws IOException {
-
 
547
        // Get user home directory
-
 
548
        String userHome = System.getProperty("user.home");
-
 
549
 
-
 
550
        // Build the file path (home + filename)
-
 
551
        File targetFile = new File(userHome, fileName);
-
 
552
 
-
 
553
        // Ensure parent directories exist (in case filename contains subfolders)
-
 
554
        if (targetFile.getParentFile() != null && !targetFile.getParentFile().exists()) {
-
 
555
            targetFile.getParentFile().mkdirs();
-
 
556
        }
-
 
557
 
-
 
558
        // Copy the stream contents
-
 
559
        try (InputStream in = source.getInputStream();
-
 
560
             OutputStream out = new FileOutputStream(targetFile)) {
-
 
561
 
-
 
562
            byte[] buffer = new byte[8192];
-
 
563
            int bytesRead;
-
 
564
            while ((bytesRead = in.read(buffer)) != -1) {
-
 
565
                out.write(buffer, 0, bytesRead);
-
 
566
            }
-
 
567
        }
-
 
568
        logger.info("Saving InputStreamSource to home directory: {}", targetFile.getAbsolutePath());
-
 
569
 
-
 
570
        return targetFile;
-
 
571
    }
-
 
572
 
509
}
573
}
510
574