-
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read the filename property from multipart request. Refactoring of fil…
…e handling classes. modified: src/main/java/org/restheart/db/GridFsDAO.java modified: src/main/java/org/restheart/handlers/injectors/BodyInjectorHandler.java
- Loading branch information
Maurizio Turatti
committed
Oct 12, 2015
1 parent
ff5d7e8
commit fc22576
Showing
2 changed files
with
107 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,10 @@ | |
* @author Andrea Di Cesare <[email protected]> | ||
*/ | ||
public class GridFsDAO implements GridFsRepository { | ||
|
||
private static final String FILENAME = "filename"; | ||
private static final String _ID = "_id"; | ||
|
||
private final MongoClient client; | ||
|
||
public GridFsDAO() { | ||
|
@@ -42,37 +46,24 @@ public GridFsDAO() { | |
|
||
@Override | ||
public int createFile( | ||
final Database db, | ||
final String dbName, | ||
final String bucketName, | ||
final Object fileId, | ||
final DBObject properties, | ||
final Database db, | ||
final String dbName, | ||
final String bucketName, | ||
final Object fileId, | ||
final DBObject properties, | ||
final File data) throws IOException, DuplicateKeyException { | ||
|
||
final String bucket = extractBucketName(bucketName); | ||
GridFS gridfs = new GridFS(db.getDB(dbName), bucket); | ||
GridFSInputFile gfsFile = gridfs.createFile(data); | ||
|
||
// remove from the properties the fields that are managed directly by the GridFs | ||
properties.removeField("_id"); | ||
Object _fileName = properties.removeField("filename"); | ||
properties.removeField("chunkSize"); | ||
properties.removeField("uploadDate"); | ||
properties.removeField("length"); | ||
properties.removeField("md5"); | ||
|
||
String fileName; | ||
if (_fileName != null && _fileName instanceof String) { | ||
fileName = (String) _fileName; | ||
} else { | ||
fileName = null; | ||
} | ||
String filename = extractFilenameFromProperties(properties); | ||
gfsFile.setFilename(filename); | ||
|
||
// add etag | ||
properties.put("_etag", new ObjectId()); | ||
|
||
gfsFile.setId(fileId); | ||
gfsFile.setFilename(fileName); | ||
|
||
properties.toMap().keySet().stream().forEach(k -> gfsFile.put((String) k, properties.get((String) k))); | ||
|
||
|
@@ -81,24 +72,33 @@ public int createFile( | |
return HttpStatus.SC_CREATED; | ||
} | ||
|
||
private String extractFilenameFromProperties(final DBObject properties) { | ||
String filename = null; | ||
if (properties != null && properties.containsField(FILENAME)) { | ||
filename = (String) properties.get(FILENAME); | ||
} | ||
|
||
return filename; | ||
} | ||
|
||
@Override | ||
public int deleteFile( | ||
final Database db, | ||
final String dbName, | ||
final String bucketName, | ||
final Object fileId, | ||
final Database db, | ||
final String dbName, | ||
final String bucketName, | ||
final Object fileId, | ||
final ObjectId requestEtag) { | ||
|
||
GridFS gridfs = new GridFS(db.getDB(dbName), extractBucketName(bucketName)); | ||
GridFSDBFile dbsfile = gridfs.findOne(new BasicDBObject("_id", fileId)); | ||
GridFSDBFile dbsfile = gridfs.findOne(new BasicDBObject(_ID, fileId)); | ||
|
||
if (dbsfile == null) { | ||
return HttpStatus.SC_NOT_FOUND; | ||
} else { | ||
int code = checkEtag(requestEtag, dbsfile); | ||
if (code == HttpStatus.SC_NO_CONTENT) { | ||
// delete file | ||
gridfs.remove(new BasicDBObject("_id", fileId)); | ||
gridfs.remove(new BasicDBObject(_ID, fileId)); | ||
} | ||
|
||
return code; | ||
|
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