Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20230403
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
20230403
Commits
25d7d4b6
Commit
25d7d4b6
authored
May 26, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Obsługa zdjęć
parent
c38317d2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
2 deletions
+85
-2
PhotoUtil.java
PC36-RestSpring/src/main/java/sklep/photo/PhotoUtil.java
+67
-0
ProductController.java
...estSpring/src/main/java/sklep/rest/ProductController.java
+18
-2
No files found.
PC36-RestSpring/src/main/java/sklep/photo/PhotoUtil.java
0 → 100644
View file @
25d7d4b6
package
sklep
.
photo
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.server.ResponseStatusException
;
@Component
public
class
PhotoUtil
{
@Value
(
"${alx.photo_dir}"
)
private
String
photoDir
;
private
static
final
String
EXT
=
".jpg"
;
public
File
getFile
(
int
productId
)
{
Path
path
=
getPath
(
productId
);
File
file
=
path
.
toFile
();
if
(
file
.
exists
())
{
return
file
;
}
else
{
throw
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
,
"Cannot read photo for product id = "
+
productId
);
}
}
public
byte
[]
readBytes
(
int
productId
)
{
Path
path
=
getPath
(
productId
);
try
{
return
Files
.
readAllBytes
(
path
);
}
catch
(
IOException
e
)
{
// System.err.println(e);
throw
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
,
"Cannot read photo for product id = "
+
productId
);
}
}
public
void
writeStream
(
int
productId
,
InputStream
inputStream
)
{
try
{
Path
path
=
getPath
(
productId
);
Files
.
copy
(
inputStream
,
path
,
StandardCopyOption
.
REPLACE_EXISTING
);
}
catch
(
Exception
e
)
{
// wypisujemy błąd, ale metoda kończy się normalnie
e
.
printStackTrace
();
}
}
public
void
writeBytes
(
int
productId
,
byte
[]
bytes
)
{
try
{
Path
path
=
getPath
(
productId
);
Files
.
write
(
path
,
bytes
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
private
Path
getPath
(
int
productId
)
{
String
fileName
=
productId
+
EXT
;
return
Paths
.
get
(
photoDir
,
fileName
);
}
}
PC36-RestSpring/src/main/java/sklep/rest/ProductController.java
View file @
25d7d4b6
...
...
@@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
import
org.springframework.web.server.ResponseStatusException
;
import
sklep.model.Product
;
import
sklep.photo.PhotoUtil
;
import
sklep.repository.ProductRepository
;
...
...
@@ -31,9 +32,11 @@ import sklep.repository.ProductRepository;
@RequestMapping
(
"/products"
)
public
class
ProductController
{
private
ProductRepository
productRepository
;
private
PhotoUtil
photoUtil
;
public
ProductController
(
ProductRepository
productRepository
)
{
public
ProductController
(
ProductRepository
productRepository
,
PhotoUtil
photoUtil
)
{
this
.
productRepository
=
productRepository
;
this
.
photoUtil
=
photoUtil
;
}
@GetMapping
...
...
@@ -46,7 +49,6 @@ public class ProductController {
return
productRepository
.
findById
(
id
).
orElseThrow
(()
->
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
));
}
// Przykład metody, która "wchodzi w szczegóły rekordu".
// To nie jest częsta praktyka, ale pokazuję to dla lepszego zrozumienia idei REST - adresy URL odpowiadają logicznej strukturze danych.
@GetMapping
(
"/{id}/price"
)
...
...
@@ -137,4 +139,18 @@ public class ProductController {
throw
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
,
"Brak produktu nr "
+
productId
);
}
}
/* Większość komunikacji w usługach REST odbywa się w formacie JSON,
* ale czasami używany jest też format XML,
* a dla niektórych danych stosujemy bezpośrednio jakiś format specjalny, np. PNG, JPG dla obrazów, PDF dla wydruków itp.
*/
@GetMapping
(
path
=
"/{id}/photo"
,
produces
=
"image/jpeg"
)
public
byte
[]
getPhoto
(
@PathVariable
(
"id"
)
int
productId
)
{
return
photoUtil
.
readBytes
(
productId
);
}
@PutMapping
(
path
=
"/{id}/photo"
,
consumes
=
"image/jpeg"
)
public
void
uploadPhoto
(
@PathVariable
(
"id"
)
int
productId
,
@RequestBody
byte
[]
bytes
)
{
photoUtil
.
writeBytes
(
productId
,
bytes
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment