88import com .ithit .webdav .server .exceptions .ServerException ;
99import com .ithit .webdav .server .resumableupload .ResumableUpload ;
1010import com .ithit .webdav .server .resumableupload .UploadProgress ;
11+ import com .sun .nio .file .ExtendedOpenOption ;
1112
1213import java .io .IOException ;
1314import java .io .InputStream ;
@@ -243,11 +244,7 @@ public void read(OutputStream out, long startIndex, long count) throws ServerExc
243244 public long write (InputStream content , String contentType , long startIndex , long totalFileLength )
244245 throws LockedException , ServerException , IOException {
245246 ensureHasToken ();
246- Path fullPath = getFullPath ();
247- if (!Files .exists (fullPath )) {
248- Files .createFile (fullPath );
249- }
250- SeekableByteChannel writer = Files .newByteChannel (fullPath , StandardOpenOption .WRITE , StandardOpenOption .CREATE );
247+ SeekableByteChannel writer = Files .newByteChannel (getFullPath (), StandardOpenOption .WRITE , StandardOpenOption .CREATE , StandardOpenOption .READ , ExtendedOpenOption .NOSHARE_DELETE );
251248 if (startIndex == 0 ) {
252249 // If we override the file we must set position to 0 because writer could be at not 0 position.
253250 writer = writer .truncate (0 );
@@ -259,22 +256,24 @@ public long write(InputStream content, String contentType, long startIndex, long
259256 byte [] inputBuffer = new byte [bufferSize ];
260257 long totalWrittenBytes = startIndex ;
261258 int readBytes ;
262- while ((readBytes = content .read (inputBuffer )) > -1 ) {
263- ByteBuffer byteBuffer = ByteBuffer .wrap (inputBuffer , 0 , readBytes );
264- try {
259+ try {
260+ while ((readBytes = content .read (inputBuffer )) > -1 ) {
261+ ByteBuffer byteBuffer ;
262+ byteBuffer = ByteBuffer .wrap (inputBuffer , 0 , readBytes );
265263 writer .write (byteBuffer );
266- } catch (Exception e ) {
267- e .printStackTrace ();
264+ totalWrittenBytes += readBytes ;
265+ }
266+ try {
267+ getEngine ().getSearchFacade ().getIndexer ().indexFile (getName (), decode (getPath ()), null , this );
268+ } catch (Exception ex ){
269+ getEngine ().getLogger ().logError ("Errors during indexing." , ex );
268270 }
269- totalWrittenBytes += readBytes ;
270- }
271- writer .close ();
272- getEngine ().getWebSocketServer ().notifyRefresh (getParent (getPath ()));
273- try {
274- getEngine ().getSearchFacade ().getIndexer ().indexFile (getName (), decode (getPath ()), null , this );
275271 } catch (Exception ex ) {
276- getEngine ().getLogger ().logError ("Errors during indexing." , ex );
272+ ex .printStackTrace ();
273+ } finally {
274+ writer .close ();
277275 }
276+ getEngine ().getWebSocketServer ().notifyRefresh (getParent (getPath ()));
278277 return totalWrittenBytes ;
279278 }
280279
@@ -307,6 +306,7 @@ public void delete() throws LockedException, MultistatusException, ServerExcepti
307306 try {
308307 Files .delete (getFullPath ());
309308 } catch (IOException e ) {
309+ getEngine ().getLogger ().logError ("Tried to delete file in use." , e );
310310 throw new ServerException (e );
311311 }
312312 getEngine ().getWebSocketServer ().notifyRefresh (getParent (getPath ()));
0 commit comments