-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#213 add initial part of the business logic
- Loading branch information
Showing
9 changed files
with
570 additions
and
18 deletions.
There are no files selected for viewing
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
224 changes: 224 additions & 0 deletions
224
src/main/java/alfio/manager/UploadedResourceManager.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,224 @@ | ||
/** | ||
* This file is part of alf.io. | ||
* | ||
* alf.io is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* alf.io is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with alf.io. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/** | ||
* This file is part of alf.io. | ||
* <p> | ||
* alf.io is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* <p> | ||
* alf.io is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* <p> | ||
* You should have received a copy of the GNU General Public License | ||
* along with alf.io. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package alfio.manager; | ||
|
||
import alfio.model.UploadedResource; | ||
import alfio.model.modification.UploadBase64FileModification; | ||
import alfio.repository.UploadedResourceRepository; | ||
import alfio.util.Json; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; | ||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | ||
import org.springframework.jdbc.core.namedparam.SqlParameterSource; | ||
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback; | ||
import org.springframework.jdbc.support.lob.DefaultLobHandler; | ||
import org.springframework.jdbc.support.lob.LobCreator; | ||
import org.springframework.jdbc.support.lob.LobHandler; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.util.StreamUtils; | ||
|
||
import javax.imageio.ImageIO; | ||
import java.awt.image.BufferedImage; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Component | ||
@Transactional | ||
@Log4j2 | ||
public class UploadedResourceManager { | ||
|
||
public static final String ATTR_IMG_WIDTH = "width"; | ||
public static final String ATTR_IMG_HEIGHT = "height"; | ||
|
||
private final NamedParameterJdbcTemplate jdbc; | ||
private final UploadedResourceRepository uploadedResourceRepository; | ||
|
||
@Autowired | ||
public UploadedResourceManager(NamedParameterJdbcTemplate jdbc, UploadedResourceRepository uploadedResourceRepository) { | ||
this.jdbc = jdbc; | ||
this.uploadedResourceRepository = uploadedResourceRepository; | ||
} | ||
|
||
public boolean hasResource(String name) { | ||
return uploadedResourceRepository.hasResource(name); | ||
} | ||
|
||
public boolean hasResource(int organizationId, String name) { | ||
return uploadedResourceRepository.hasResource(organizationId, name); | ||
} | ||
|
||
public boolean hasResource(int organizationId, int eventId, String name) { | ||
return uploadedResourceRepository.hasResource(organizationId, eventId, name); | ||
} | ||
|
||
|
||
public void outputResource(String name, OutputStream out) { | ||
SqlParameterSource param = new MapSqlParameterSource("name", name); | ||
jdbc.query(uploadedResourceRepository.fileContentTemplate(name), param, rs -> { | ||
try (InputStream is = rs.getBinaryStream("content")) { | ||
StreamUtils.copy(is, out); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Error while copying data", e); | ||
} | ||
}); | ||
} | ||
|
||
public void outputResource(int organizationId, String name, OutputStream out) { | ||
SqlParameterSource param = new MapSqlParameterSource("name", name).addValue("organizationId", organizationId); | ||
jdbc.query(uploadedResourceRepository.fileContentTemplate(organizationId, name), param, rs -> { | ||
try (InputStream is = rs.getBinaryStream("content")) { | ||
StreamUtils.copy(is, out); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Error while copying data", e); | ||
} | ||
}); | ||
} | ||
|
||
public void outputResource(int organizationId, int eventId, String name, OutputStream out) { | ||
SqlParameterSource param = new MapSqlParameterSource("name", name).addValue("organizationId", organizationId).addValue("eventId", eventId); | ||
jdbc.query(uploadedResourceRepository.fileContentTemplate(organizationId, eventId, name), param, rs -> { | ||
try (InputStream is = rs.getBinaryStream("content")) { | ||
StreamUtils.copy(is, out); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Error while copying data", e); | ||
} | ||
}); | ||
} | ||
|
||
public int saveResource(UploadBase64FileModification file) { | ||
if (hasResource(file.getName())) { | ||
uploadedResourceRepository.delete(file.getName()); | ||
} | ||
LobHandler lobHandler = new DefaultLobHandler(); | ||
return jdbc.getJdbcOperations().execute(uploadedResourceRepository.uploadTemplate(file.getName()), | ||
new AbstractLobCreatingPreparedStatementCallback(lobHandler) { | ||
@Override | ||
protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException { | ||
setFileValues(ps, lobCreator, file, 1); | ||
} | ||
}); | ||
|
||
} | ||
|
||
public int saveResource(int organizationId, UploadBase64FileModification file) { | ||
if (hasResource(organizationId, file.getName())) { | ||
uploadedResourceRepository.delete(organizationId, file.getName()); | ||
} | ||
LobHandler lobHandler = new DefaultLobHandler(); | ||
return jdbc.getJdbcOperations().execute(uploadedResourceRepository.uploadTemplate(organizationId, file.getName()), | ||
new AbstractLobCreatingPreparedStatementCallback(lobHandler) { | ||
@Override | ||
protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException { | ||
ps.setInt(2, organizationId); | ||
setFileValues(ps, lobCreator, file, 2); | ||
} | ||
}); | ||
} | ||
|
||
public int saveResource(int organizationId, int eventId, UploadBase64FileModification file) { | ||
if (hasResource(organizationId, eventId, file.getName())) { | ||
uploadedResourceRepository.delete(organizationId, eventId, file.getName()); | ||
} | ||
LobHandler lobHandler = new DefaultLobHandler(); | ||
return jdbc.getJdbcOperations().execute(uploadedResourceRepository.uploadTemplate(organizationId, eventId, file.getName()), | ||
new AbstractLobCreatingPreparedStatementCallback(lobHandler) { | ||
@Override | ||
protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException { | ||
ps.setInt(2, organizationId); | ||
ps.setInt(3, eventId); | ||
setFileValues(ps, lobCreator, file, 3); | ||
} | ||
}); | ||
|
||
} | ||
|
||
private static void setFileValues(PreparedStatement ps, LobCreator lobCreator, UploadBase64FileModification file, int baseIndex) throws SQLException { | ||
ps.setString(1, file.getName()); | ||
|
||
ps.setLong(baseIndex + 1, file.getFile().length); | ||
lobCreator.setBlobAsBytes(ps, baseIndex + 2, file.getFile()); | ||
ps.setString(baseIndex + 3, file.getType()); | ||
ps.setString(baseIndex + 4, Json.GSON.toJson(getAttributes(file))); | ||
} | ||
|
||
public void deleteResource(String name) { | ||
uploadedResourceRepository.delete(name); | ||
} | ||
|
||
public void deleteResource(int organizationId, String name) { | ||
uploadedResourceRepository.delete(organizationId, name); | ||
} | ||
|
||
public void deleteResource(int organizationId, int eventId, String name) { | ||
uploadedResourceRepository.delete(organizationId, eventId, name); | ||
} | ||
|
||
public List<UploadedResource> findAll() { | ||
return uploadedResourceRepository.findAll(); | ||
} | ||
|
||
public List<UploadedResource> findAll(int organizationId) { | ||
return uploadedResourceRepository.findAll(organizationId); | ||
} | ||
|
||
public List<UploadedResource> findAll(int organizationId, int eventId) { | ||
return uploadedResourceRepository.findAll(organizationId, eventId); | ||
} | ||
|
||
private static Map<String, String> getAttributes(UploadBase64FileModification file) { | ||
if (!StringUtils.startsWith(file.getType(), "image/")) { | ||
return file.getAttributes(); | ||
} | ||
|
||
try { | ||
BufferedImage image = ImageIO.read(new ByteArrayInputStream(file.getFile())); | ||
Map<String, String> attributes = new HashMap<>(file.getAttributes()); | ||
attributes.put(ATTR_IMG_WIDTH, String.valueOf(image.getWidth())); | ||
attributes.put(ATTR_IMG_HEIGHT, String.valueOf(image.getHeight())); | ||
return attributes; | ||
} catch (IOException e) { | ||
log.error("error while processing image: ", e); | ||
return file.getAttributes(); | ||
} | ||
} | ||
} |
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,56 @@ | ||
/** | ||
* This file is part of alf.io. | ||
* | ||
* alf.io is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* alf.io is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with alf.io. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package alfio.model; | ||
|
||
|
||
import alfio.util.Json; | ||
import ch.digitalfondue.npjt.ConstructorAnnotationRowMapper.Column; | ||
import com.google.gson.reflect.TypeToken; | ||
import lombok.Getter; | ||
|
||
import java.util.Collections; | ||
import java.util.Date; | ||
import java.util.Map; | ||
|
||
@Getter | ||
public class UploadedResource { | ||
|
||
private final String name; | ||
private final Integer organizationId; | ||
private final Integer eventId; | ||
private final String contentType; | ||
private final int contentSize; | ||
private final Date creationTime; | ||
private final Map<String, String> attributes; | ||
|
||
public UploadedResource(@Column("name") String name, | ||
@Column("organization_id_fk") Integer organizationId, | ||
@Column("event_id_fk") Integer eventId, | ||
@Column("content_type") String contentType, | ||
@Column("content_size") int contentSize, | ||
@Column("creation_time") Date creationTime, | ||
@Column("attributes") String attributes) { | ||
this.name = name; | ||
this.organizationId = organizationId; | ||
this.eventId = eventId; | ||
this.contentType = contentType; | ||
this.contentSize = contentSize; | ||
this.creationTime = creationTime; | ||
Map<String, String> parsed = Json.GSON.fromJson(attributes, new TypeToken<Map<String, String>>() {}.getType()); | ||
this.attributes = parsed != null ? parsed : Collections.emptyMap(); | ||
} | ||
} |
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
Oops, something went wrong.