1
1
package com.wizpanda.file.api
2
2
3
+ import com.wizpanda.file.ConfigHelper
3
4
import com.wizpanda.file.StoredFile
4
5
import com.wizpanda.file.service.AmazonS3UploaderService
5
6
import grails.util.Environment
@@ -9,7 +10,10 @@ import org.jclouds.ContextBuilder
9
10
import org.jclouds.aws.s3.AWSS3Client
10
11
import org.jclouds.blobstore.BlobStore
11
12
import org.jclouds.blobstore.BlobStoreContext
13
+ import org.jclouds.http.HttpResponseException
14
+ import org.jclouds.s3.domain.CannedAccessPolicy
12
15
import org.jclouds.s3.domain.internal.MutableObjectMetadataImpl
16
+ import org.jclouds.s3.options.CopyObjectOptions
13
17
14
18
import javax.activation.MimetypesFileTypeMap
15
19
@@ -57,6 +61,15 @@ abstract class AmazonS3Api extends AbstractStorageApi {
57
61
return name + " ." + extension
58
62
}
59
63
64
+ @Override
65
+ String getFileName (StoredFile file ) {
66
+ String name = UUID . randomUUID(). toString()
67
+ String originalFileName = file. originalName
68
+ String extension = GrailsStringUtils . substringAfterLast(originalFileName, " ." )
69
+
70
+ return name + " ." + extension
71
+ }
72
+
60
73
String getContainerName () {
61
74
String name = service. container
62
75
if (Environment . current != Environment . PRODUCTION ) {
@@ -85,13 +98,54 @@ abstract class AmazonS3Api extends AbstractStorageApi {
85
98
this . authenticate()
86
99
87
100
if (! client. objectExists(container, fileName)) {
88
- log. warn " File not present in the S3 bucket."
101
+ log. warn " File not present in the S3 bucket, deleting the Stored file instance."
102
+
103
+ file. delete()
104
+ this . close()
105
+
89
106
return
90
107
}
91
108
92
109
client. deleteObject(container, fileName)
93
- file. delete(flush : true )
110
+ file. delete()
94
111
95
112
this . close()
96
113
}
114
+
115
+ @Override
116
+ StoredFile cloneStoredFile (StoredFile file , String newGroupName ) {
117
+ StoredFile clonedFile = new StoredFile ()
118
+ clonedFile. originalName = file. originalName
119
+ clonedFile. groupName = newGroupName
120
+ clonedFile. size = file. size
121
+ clonedFile. name = getFileName(file)
122
+
123
+ String currentContainer = ConfigHelper . getGroup(file. groupName). container ?:
124
+ ConfigHelper . getFlatConfig(" global.amazon.container" )
125
+
126
+ String newContainer = getContainerName()
127
+
128
+ CopyObjectOptions fileOptions = new CopyObjectOptions ()
129
+ // For now using the same policy of original file, it gets changed to private scope if not overridden.
130
+ fileOptions. overrideAcl(CannedAccessPolicy . PUBLIC_READ )
131
+
132
+ try {
133
+ this . authenticate()
134
+ client. copyObject(currentContainer, file. name, newContainer, clonedFile. name, fileOptions)
135
+ clonedFile. url = client. getObject(newContainer, clonedFile. name, null ). metadata. uri
136
+
137
+ this . gormFile = clonedFile
138
+ this . gormFile. uploadedOn = new Date ()
139
+
140
+ this . saveGORMFile()
141
+
142
+ log. info " Successfully cloned StoredFile: ${ file} as ${ this.gormFile} "
143
+
144
+ return this . gormFile
145
+ } catch (HttpResponseException hre) {
146
+ log. warn ' Could not copy StoredFile!' , hre
147
+ } finally {
148
+ this . close()
149
+ }
150
+ }
97
151
}
0 commit comments