3434import com .owncloud .android .lib .common .operations .RemoteOperationResult ;
3535import com .owncloud .android .lib .common .utils .Log_OC ;
3636
37+ import java .text .DateFormat ;
38+ import java .text .SimpleDateFormat ;
39+ import java .util .Calendar ;
40+ import java .util .Locale ;
41+
3742/**
3843 * Creates a new share. This allows sharing with a user or group or as a link.
3944 */
4045public class CreateRemoteShareOperation extends RemoteOperation {
4146
4247 private static final String TAG = CreateRemoteShareOperation .class .getSimpleName ();
4348
49+ private static final String PARAM_NAME = "name" ;
50+ private static final String PARAM_PASSWORD = "password" ;
51+ private static final String PARAM_EXPIRATION_DATE = "expireDate" ;
52+ private static final String PARAM_PUBLIC_UPLOAD = "publicUpload" ;
4453 private static final String PARAM_PATH = "path" ;
4554 private static final String PARAM_SHARE_TYPE = "shareType" ;
4655 private static final String PARAM_SHARE_WITH = "shareWith" ;
47- private static final String PARAM_PUBLIC_UPLOAD = "publicUpload" ;
48- private static final String PARAM_PASSWORD = "password" ;
4956 private static final String PARAM_PERMISSIONS = "permissions" ;
57+ private static final String FORMAT_EXPIRATION_DATE = "yyyy-MM-dd" ;
5058
5159 private String mRemoteFilePath ;
5260 private ShareType mShareType ;
5361 private String mShareWith ;
54- private boolean mPublicUpload ;
62+ private boolean mGetShareDetails ;
63+
64+ /**
65+ * Name to set for the public link
66+ */
67+ private String mName = "" ;
68+
69+ /**
70+ * Password to set for the public link
71+ */
5572 private String mPassword ;
73+
74+ /**
75+ * Expiration date to set for the public link
76+ */
77+ private long mExpirationDateInMillis = 0 ;
78+
79+ /**
80+ * Access permissions for the file bound to the share
81+ */
5682 private int mPermissions ;
57- private boolean mGetShareDetails ;
83+
84+ /**
85+ * Upload permissions for the public link (only folders)
86+ */
87+ private Boolean mPublicUpload ;
5888
5989 /**
6090 * Constructor
@@ -77,12 +107,12 @@ public class CreateRemoteShareOperation extends RemoteOperation {
77107 * For instance, for Re-Share, delete, read, update, add 16+8+2+1 = 27.
78108 */
79109 public CreateRemoteShareOperation (
80- String remoteFilePath ,
81- ShareType shareType ,
82- String shareWith ,
83- boolean publicUpload ,
84- String password ,
85- int permissions
110+ String remoteFilePath ,
111+ ShareType shareType ,
112+ String shareWith ,
113+ boolean publicUpload ,
114+ String password ,
115+ int permissions
86116 ) {
87117
88118 mRemoteFilePath = remoteFilePath ;
@@ -94,6 +124,59 @@ public CreateRemoteShareOperation(
94124 mGetShareDetails = false ; // defaults to false for backwards compatibility
95125 }
96126
127+
128+ /**
129+ * Set name to create in Share resource. Ignored by servers previous to version 10.0.0
130+ *
131+ * @param name Name to set to the target share.
132+ * Null or empty string result in no value set for the name.
133+ */
134+ public void setName (String name ) {
135+ this .mName = (name == null ) ? "" : name ;
136+ }
137+
138+ /**
139+ * Set password to create in Share resource.
140+ *
141+ * @param password Password to set to the target share.
142+ * Null or empty string result in no value set for the password.
143+ */
144+ public void setPassword (String password ) {
145+ mPassword = password ;
146+ }
147+
148+
149+ /**
150+ * Set expiration date to create in Share resource.
151+ *
152+ * @param expirationDateInMillis Expiration date to set to the target share.
153+ * Zero or negative value results in no value sent for expiration date.
154+ */
155+ public void setExpirationDate (long expirationDateInMillis ) {
156+ mExpirationDateInMillis = expirationDateInMillis ;
157+ }
158+
159+
160+ /**
161+ * Set permissions to create in Share resource.
162+ *
163+ * @param permissions Permissions to set to the target share.
164+ * Values <= 0 result in value set to the permissions.
165+ */
166+ public void setPermissions (int permissions ) {
167+ mPermissions = permissions ;
168+ }
169+
170+ /**
171+ * * Enable upload permissions to create in Share resource.
172+ * *
173+ * * @param publicUpload Upload permission to set to the target share.
174+ * * Null results in no update applied to the upload permission.
175+ */
176+ public void setPublicUpload (Boolean publicUpload ) {
177+ mPublicUpload = publicUpload ;
178+ }
179+
97180 public boolean isGettingShareDetails () {
98181 return mGetShareDetails ;
99182 }
@@ -114,11 +197,24 @@ protected RemoteOperationResult run(OwnCloudClient client) {
114197 post = new PostMethod (client .getBaseUri () + ShareUtils .SHARING_API_PATH );
115198
116199 post .setRequestHeader ("Content-Type" ,
117- "application/x-www-form-urlencoded; charset=utf-8" ); // necessary for special characters
200+ "application/x-www-form-urlencoded; charset=utf-8" ); // necessary for special characters
118201
119202 post .addParameter (PARAM_PATH , mRemoteFilePath );
120203 post .addParameter (PARAM_SHARE_TYPE , Integer .toString (mShareType .getValue ()));
121204 post .addParameter (PARAM_SHARE_WITH , mShareWith );
205+
206+ if (mName .length () > 0 ) {
207+ post .addParameter (PARAM_NAME , mName );
208+ }
209+
210+ if (mExpirationDateInMillis > 0 ) {
211+ DateFormat dateFormat = new SimpleDateFormat (FORMAT_EXPIRATION_DATE , Locale .getDefault ());
212+ Calendar expirationDate = Calendar .getInstance ();
213+ expirationDate .setTimeInMillis (mExpirationDateInMillis );
214+ String formattedExpirationDate = dateFormat .format (expirationDate .getTime ());
215+ post .addParameter (PARAM_EXPIRATION_DATE , formattedExpirationDate );
216+ }
217+
122218 if (mPublicUpload ) {
123219 post .addParameter (PARAM_PUBLIC_UPLOAD , Boolean .toString (true ));
124220 }
@@ -137,7 +233,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
137233 String response = post .getResponseBodyAsString ();
138234
139235 ShareToRemoteOperationResultParser parser = new ShareToRemoteOperationResultParser (
140- new ShareXMLParser ()
236+ new ShareXMLParser ()
141237 );
142238 parser .setOneOrMoreSharesRequired (true );
143239 parser .setOwnCloudVersion (client .getOwnCloudVersion ());
@@ -148,7 +244,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
148244 // retrieve more info - POST only returns the index of the new share
149245 OCShare emptyShare = (OCShare ) result .getData ().get (0 );
150246 GetRemoteShareOperation getInfo = new GetRemoteShareOperation (
151- emptyShare .getRemoteId ()
247+ emptyShare .getRemoteId ()
152248 );
153249 result = getInfo .execute (client );
154250 }
0 commit comments