-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from rohit-56/upload-blog-cover
Upload blog cover
- Loading branch information
Showing
13 changed files
with
198 additions
and
15 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/main/java/com/example/BloggerApp/common/utils/Base64Converter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.example.BloggerApp.common.utils; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
public class Base64Converter { | ||
|
||
public static byte[] decodeBase64(String base64String) throws UnsupportedEncodingException { | ||
return Base64.getMimeDecoder().decode(base64String.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
public static String encodeBase64(byte[] image){ | ||
return Base64.getEncoder().encodeToString(image); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/example/BloggerApp/common/utils/ImageUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.example.BloggerApp.common.utils; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.util.zip.Deflater; | ||
import java.util.zip.Inflater; | ||
|
||
public class ImageUtils { | ||
|
||
public static byte[] compressImage(byte[] data) { | ||
Deflater deflater = new Deflater(); | ||
deflater.setLevel(Deflater.BEST_COMPRESSION); | ||
deflater.setInput(data); | ||
deflater.finish(); | ||
|
||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); | ||
byte[] tmp = new byte[4*1024]; | ||
while (!deflater.finished()) { | ||
int size = deflater.deflate(tmp); | ||
outputStream.write(tmp, 0, size); | ||
} | ||
try { | ||
outputStream.close(); | ||
} catch (Exception ignored) { | ||
} | ||
return outputStream.toByteArray(); | ||
} | ||
|
||
|
||
|
||
public static byte[] decompressImage(byte[] data) { | ||
Inflater inflater = new Inflater(); | ||
inflater.setInput(data); | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); | ||
byte[] tmp = new byte[4*1024]; | ||
try { | ||
while (!inflater.finished()) { | ||
int count = inflater.inflate(tmp); | ||
outputStream.write(tmp, 0, count); | ||
} | ||
outputStream.close(); | ||
} catch (Exception ignored) { | ||
} | ||
return outputStream.toByteArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,4 @@ public class CreateBlogRequest { | |
|
||
private String body; | ||
|
||
private String imageCover; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/com/example/BloggerApp/models/ImageEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.example.BloggerApp.models; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import javax.persistence.CascadeType; | ||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.Lob; | ||
import javax.persistence.OneToOne; | ||
|
||
@Entity | ||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor(force = true) | ||
@Builder | ||
public class ImageEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE) | ||
@Column(name = "id", nullable = false) | ||
private Long id; | ||
|
||
private String name; | ||
|
||
private String type; | ||
|
||
@Lob | ||
private byte[] imageData; | ||
|
||
|
||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/BloggerApp/repository/ImageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.example.BloggerApp.repository; | ||
|
||
import com.example.BloggerApp.models.ImageEntity; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface ImageRepository extends JpaRepository<ImageEntity,Long> { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/example/BloggerApp/service/ImageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.example.BloggerApp.service; | ||
|
||
import com.example.BloggerApp.models.ImageEntity; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
public interface ImageService { | ||
|
||
ImageEntity uploadImage(MultipartFile file); | ||
|
||
ImageEntity downloadImage(Long imageId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/com/example/BloggerApp/service/impl/ImageServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.example.BloggerApp.service.impl; | ||
|
||
import com.example.BloggerApp.common.utils.ImageUtils; | ||
import com.example.BloggerApp.models.ImageEntity; | ||
import com.example.BloggerApp.repository.ImageRepository; | ||
import com.example.BloggerApp.service.ImageService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
|
||
@Service | ||
public class ImageServiceImpl implements ImageService { | ||
|
||
@Autowired | ||
private ImageRepository imageRepository; | ||
@Override | ||
public ImageEntity uploadImage(MultipartFile file) { | ||
ImageEntity imageEntity = new ImageEntity(); | ||
imageEntity.setName(file.getName()); | ||
imageEntity.setType(file.getContentType()); | ||
try { | ||
imageEntity.setImageData(ImageUtils.compressImage(file.getBytes())); | ||
}catch (Exception ex){ | ||
System.out.println(ex.getMessage()); | ||
} | ||
|
||
return imageRepository.save(imageEntity); | ||
} | ||
|
||
@Override | ||
public ImageEntity downloadImage(Long imageId) { | ||
return imageRepository.findById(imageId).get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters