Skip to content

Commit

Permalink
[#24][#25] Add new method for API media, add some check before execut…
Browse files Browse the repository at this point in the history
…e actions
  • Loading branch information
Hugo Bernardi committed Jun 8, 2016
1 parent 0902969 commit b9fc45f
Show file tree
Hide file tree
Showing 20 changed files with 344 additions and 132 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ local.properties
bin/
gen/

db/
public/
/dashboard-back/db/
/dashboard-back/public/
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface MediaDao {
* @param uuid the media uuid
* @return the retrieved media or null if no match
*/
MediaMetadata get(String uuid);
MediaMetadata get(String uuid, String uuidBundle);

/**
* Retrieve all medias from DB.
Expand All @@ -38,5 +38,5 @@ public interface MediaDao {
*
* @param uuid The bundle uuid
*/
boolean delete(String uuid);
boolean delete(String uuid, String uuidBundle);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public void init() {
}

@Override
public MediaMetadata get(String uuid) {
File dataFile = getBundleFile(uuid);
public MediaMetadata get(String uuid, String uuidBundle) {
File dataFile = getMediaFile(uuidBundle + "/" + uuid);
return readBundleFromFile(dataFile);
}

Expand All @@ -51,17 +51,17 @@ public MediaMetadata save(MediaMetadata mediaMetadata) {
if (mediaMetadata.getUuid() == null) {
mediaMetadata.setUuid(UUID.randomUUID().toString());
}
File dest = getBundleFile(mediaMetadata.getUuid());
File dest = getMediaFile(mediaMetadata.getBundleTag() + "/" + mediaMetadata.getUuid());
YamlUtils.store(mediaMetadata, dest);
return mediaMetadata;
}

@Override
public boolean delete(String uuid) {
return YamlUtils.delete(mediaDatabasePath.resolve(uuid + ".yaml").toFile());
public boolean delete(String uuid, String uuidBundle) {
return YamlUtils.delete(mediaDatabasePath.resolve(uuidBundle + "/" + uuid + ".yaml").toFile());
}

private File getBundleFile(String uuid) {
private File getMediaFile(String uuid) {
String dataFileName = uuid + ".yaml";
return mediaDatabasePath.resolve(dataFileName).toFile();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static void store(Object o, File dest) {
FileOutputStream fos = null;
try {
if (!dest.exists()) {
dest.getParentFile().mkdirs();
dest.createNewFile();
}
fos = new FileOutputStream(dest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ public class BundleDtoMapperImpl implements MapperDto<BundleMetadata, BundleMeta

@Override
public BundleMetadataDto toDto(BundleMetadata model) {
return new BundleMetadataDto.Builder()
return model != null ? new BundleMetadataDto.Builder()
.uuid(model.getUuid())
.name(model.getName())
.validity(validityDtoMapper.toDto(model.getValidity()))
.build();
.build() : null;
}

@Override
public BundleMetadata fromDto(BundleMetadataDto dto) {
return new BundleMetadata.Builder()
return dto != null ? new BundleMetadata.Builder()
.uuid(dto.getUuid())
.name(dto.getName())
.validity(validityDtoMapper.fromDto(dto.getValidity()))
.build();
.build() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ public class MediaDtoMapperImpl implements MapperDto<MediaMetadata, MediaMetadat

@Override
public MediaMetadataDto toDto(MediaMetadata model) {
return new MediaMetadataDto.Builder()
return model != null ? new MediaMetadataDto.Builder()
.uuid(model.getUuid())
.name(model.getName())
.duration(model.getDuration())
.mediaType(model.getMediaType())
.validity(validityDtoMapper.toDto(model.getValidity()))
.url(model.getUrl())
.uuidBundle(model.getBundleTag())
.build();
.build() : null;
}

@Override
public MediaMetadata fromDto(MediaMetadataDto dto) {
return new MediaMetadata.Builder()
return dto != null ? new MediaMetadata.Builder()
.uuid(dto.getUuid())
.name(dto.getName())
.duration(dto.getDuration())
.mediaType(MediaType.getMediaType(dto.getMediaType()))
.validity(validityDtoMapper.fromDto(dto.getValidity()))
.url(dto.getUrl())
.bundleTag(dto.getUuidBundle())
.build();
.build() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@ public class ValidityDtoMapperImpl implements MapperDto<Validity, ValidityDto> {

@Override
public ValidityDto toDto(Validity model) {
if (model == null) {
return null;
}
return new ValidityDto(model.getStart().format(FORMATTER), model.getEnd().format(FORMATTER));
return model != null ? new ValidityDto(model.getStart().format(FORMATTER), model.getEnd().format(FORMATTER)) : null;
}

@Override
public Validity fromDto(ValidityDto dto) {
if (dto == null) {
return null;
}
return new Validity.Builder()
return dto != null ? new Validity.Builder()
.start(LocalDateTime.parse(dto.getStart(), FORMATTER))
.end(LocalDateTime.parse(dto.getEnd(), FORMATTER))
.build();
.build() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@ConfigurationProperties
public class DashboardProperties {
private String basePath = System.getProperty("user.dir") + "/db";
private String baseResources = System.getProperty("user.dir") + "/public";
private String apiKey = "default";
private String adminLogin = "admin";
private String adminPassword = "admin";
Expand Down Expand Up @@ -61,6 +62,10 @@ public String getBaseUrl() {
return baseUrl;
}

public String getBaseResources() {
return baseResources;
}

public void setBaseUrl(String baseUrl) {
// Strip last slash if provided to avoid mapping issues.
if (baseUrl.endsWith("/")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package com.excilys.shooflers.dashboard.server.rest;

import com.excilys.shooflers.dashboard.server.dao.RevisionDao;
import com.excilys.shooflers.dashboard.server.dto.BundleMetadataDto;
import com.excilys.shooflers.dashboard.server.dto.mapper.BundleDtoMapperImpl;
import com.excilys.shooflers.dashboard.server.security.annotation.RequireValidApiKey;
import com.excilys.shooflers.dashboard.server.security.annotation.RequireValidUser;
import com.excilys.shooflers.dashboard.server.service.BundleService;
import com.excilys.shooflers.dashboard.server.service.RevisionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

Expand All @@ -26,10 +21,7 @@ public class BundleController {
private BundleService bundleService;

@Autowired
private BundleDtoMapperImpl mapper;

@Autowired
private RevisionDao revisionDao;
private RevisionService revisionService;

/**
* Get all Bundle.
Expand All @@ -39,8 +31,8 @@ public class BundleController {
@RequestMapping(method = RequestMethod.GET)
@RequireValidApiKey
public List<BundleMetadataDto> getAll() {
List<BundleMetadataDto> bundles = mapper.toListDto(bundleService.getAll());
bundles.forEach(b -> b.setRevision(revisionDao.getLatest()));
List<BundleMetadataDto> bundles = bundleService.getAll();
bundles.forEach(b -> b.setRevision(revisionService.getLatest()));
return bundles;
}

Expand All @@ -52,7 +44,7 @@ public List<BundleMetadataDto> getAll() {
@RequireValidUser
@RequestMapping(method = RequestMethod.POST)
public BundleMetadataDto save(@RequestBody BundleMetadataDto bundleMetadataDto) {
return mapper.toDto(bundleService.create(mapper.fromDto(bundleMetadataDto)));
return bundleService.create(bundleMetadataDto);
}

/**
Expand All @@ -64,7 +56,7 @@ public BundleMetadataDto save(@RequestBody BundleMetadataDto bundleMetadataDto)
@RequestMapping(value = "{uuid}", method = RequestMethod.GET)
@RequireValidApiKey
public BundleMetadataDto get(@PathVariable("uuid") String uuid) {
return mapper.toDto(bundleService.get(uuid));
return bundleService.get(uuid);
}

/**
Expand All @@ -76,7 +68,7 @@ public BundleMetadataDto get(@PathVariable("uuid") String uuid) {
@RequestMapping(method = RequestMethod.PUT)
@RequireValidUser
public BundleMetadataDto update(@RequestBody BundleMetadataDto bundle) {
return mapper.toDto(bundleService.update(mapper.fromDto(bundle)));
return bundleService.update(bundle);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.excilys.shooflers.dashboard.server.rest;

import com.excilys.shooflers.dashboard.server.dao.MediaDao;
import com.excilys.shooflers.dashboard.server.dao.RevisionDao;
import com.excilys.shooflers.dashboard.server.dto.MediaMetadataDto;
import com.excilys.shooflers.dashboard.server.dto.mapper.MediaDtoMapperImpl;
import com.excilys.shooflers.dashboard.server.model.Revision;
import com.excilys.shooflers.dashboard.server.model.type.MediaType;
import com.excilys.shooflers.dashboard.server.property.DashboardProperties;
import com.excilys.shooflers.dashboard.server.rest.utils.FileHelper;
import com.excilys.shooflers.dashboard.server.security.annotation.RequireValidApiKey;
import com.excilys.shooflers.dashboard.server.security.annotation.RequireValidUser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.excilys.shooflers.dashboard.server.service.BundleService;
import com.excilys.shooflers.dashboard.server.service.MediaService;
import com.excilys.shooflers.dashboard.server.service.RevisionService;
import com.excilys.shooflers.dashboard.server.service.exception.BundleNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -19,7 +19,7 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
Expand All @@ -30,63 +30,63 @@
public class MediaController {

@Autowired
private MediaDao mediaDao;
private RevisionService revisionService;

@Autowired
private MediaDtoMapperImpl mapper;
private MediaService mediaService;

@Autowired
private RevisionDao revisionDao;
private DashboardProperties props;

@Autowired
private DashboardProperties props;
private BundleService bundleService;

@RequestMapping(value = "{uuid}", method = RequestMethod.GET)
@RequireValidApiKey
public MediaMetadataDto get(@PathVariable("uuid") String uuid) {
return mapper.toDto(mediaDao.get(uuid));
@RequestMapping(value = "{uuidbundle}/{uuid}", method = RequestMethod.GET)
public MediaMetadataDto get(@PathVariable("uuidbundle") String uuidBundle, @PathVariable("uuid") String uuid) {
return mediaService.get(uuid, uuidBundle);
}

@RequireValidApiKey
@RequestMapping(value = "{uuidbundle}", method = RequestMethod.GET)
public List<MediaMetadataDto> getAllByBundle(@PathVariable("uuidbundle") String uuidBundle) {
return mediaService.getByBundle(uuidBundle);
}

@RequestMapping(method = RequestMethod.POST)
@RequireValidUser
public MediaMetadataDto save(@RequestParam("media") String media, @RequestParam("file") MultipartFile multipartFile) {
ObjectMapper objectMapper = new ObjectMapper();
@RequestMapping(method = RequestMethod.POST)
public MediaMetadataDto save(@RequestParam("media") String json, @RequestParam("file") MultipartFile multipartFile) {
MediaMetadataDto media = mediaService.fromJson(json);

// Save media
MediaType mediaType = MediaType.getMediaType(media.getMediaType());

// Mapping json to object, if json is malformed, stop
MediaMetadataDto mediaMetadataDto;
try {
mediaMetadataDto = objectMapper.readValue(media, MediaMetadataDto.class);
} catch (IOException e) {
throw new IllegalArgumentException(e);
// If bundle doesn't exist, abort
if (bundleService.get(media.getUuidBundle()) == null) {
throw new BundleNotFoundException("Bundle not found, media not added");
}

// Save media
MediaType mediaType = MediaType.getMediaType(mediaMetadataDto.getMediaType());
// Not expected a file to save, directly create media with url set
if (mediaType == MediaType.WEB_SITE || mediaType == MediaType.WEB_VIDEO) {
mediaMetadataDto = mapper.toDto(mediaDao.save(mapper.fromDto(mediaMetadataDto)));
media = mediaService.save(media);
} else {
// Expect a file with a correct extension
if (multipartFile.isEmpty() || mediaType == MediaType.NONE || MediaType.getMediaType(multipartFile.getContentType()) == MediaType.NONE) {
throw new IllegalArgumentException("File extension not recognized");
}

mediaMetadataDto.setUuid(UUID.randomUUID().toString());
String fileName = FileHelper.saveFile(multipartFile, mediaMetadataDto.getUuid());
if (fileName != null) {
mediaMetadataDto.setUrl(props.getBaseUrl() + "/" + fileName);
mediaMetadataDto = mapper.toDto(mediaDao.save(mapper.fromDto(mediaMetadataDto)));
media.setUuid(UUID.randomUUID().toString());
if (FileHelper.saveFile(multipartFile, media, props.getBaseUrl())) {
media = mediaService.save(media);
}
}

// Create a new revision
Revision revision = new Revision();
revision.setRevision(revisionDao.getLatest() + 1);
revision.setType(Revision.Type.MEDIA);
revision.setTarget(mediaMetadataDto.getUuid());
revision.setAction(Revision.Action.ADD);
revisionDao.save(revision);
mediaMetadataDto.setRevision(revision.getRevision());
return mediaMetadataDto;
media.setRevision(revisionService.add(Revision.Action.ADD, media.getUuid(), Revision.Type.MEDIA, null).getRevision());
return media;
}

@RequireValidUser
@RequestMapping(value = "{uuidbundle}/{uuid}", method = RequestMethod.DELETE)
public void delete(@PathVariable("uuidbundle") String uuidBundle, @PathVariable("uuid") String uuid) {
if (mediaService.delete(uuid, uuidBundle)) {
// Create a new revision
revisionService.add(Revision.Action.DELETE, uuid, Revision.Type.MEDIA, null);
}
}
}
Loading

0 comments on commit b9fc45f

Please sign in to comment.