Skip to content

Commit b9a5016

Browse files
committed
Refactored much of the logic in FileMetadata constructor. Directory.size will return 0
1 parent 9618aa3 commit b9a5016

File tree

1 file changed

+49
-54
lines changed

1 file changed

+49
-54
lines changed

src/wp/File.cs

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/*
2-
Licensed under the Apache License, Version 2.0 (the "License");
3-
you may not use this file except in compliance with the License.
4-
You may obtain a copy of the License at
5-
6-
http://www.apache.org/licenses/LICENSE-2.0
7-
8-
Unless required by applicable law or agreed to in writing, software
9-
distributed under the License is distributed on an "AS IS" BASIS,
10-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11-
See the License for the specific language governing permissions and
12-
limitations under the License.
2+
Licensed under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License.
4+
You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
1313
*/
1414

1515
using System;
@@ -233,59 +233,50 @@ public class FileMetadata
233233

234234
public FileMetadata(string filePath)
235235
{
236+
if (string.IsNullOrEmpty(filePath))
237+
{
238+
throw new FileNotFoundException("File doesn't exist");
239+
}
240+
241+
this.FullPath = filePath;
242+
this.Size = 0;
243+
this.FileName = string.Empty;
244+
236245
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
237246
{
238247
bool IsFile = isoFile.FileExists(filePath);
239248
bool IsDirectory = isoFile.DirectoryExists(filePath);
240249

241-
if (string.IsNullOrEmpty(filePath))
242-
{
243-
throw new FileNotFoundException("File doesn't exist");
244-
}
245-
else if (!IsFile && !IsDirectory)
246-
{
247-
// attempt to get it from the resources
248-
if (filePath.IndexOf("www") == 0)
250+
if (!IsDirectory)
249251
{
250-
Uri fileUri = new Uri(filePath, UriKind.Relative);
251-
StreamResourceInfo streamInfo = Application.GetResourceStream(fileUri);
252-
if (streamInfo != null)
252+
if (!IsFile) // special case, if isoFile cannot find it, it might still be part of the app-package
253253
{
254-
this.Size = streamInfo.Stream.Length;
255-
this.FileName = filePath.Substring(filePath.LastIndexOf("/") + 1);
256-
this.FullPath = filePath;
257-
}
258-
}
259-
else
260-
{
261-
throw new FileNotFoundException("File doesn't exist");
262-
}
263-
}
264-
else
265-
{
266-
//TODO get file size the other way if possible
267-
//Use StreamResourceInfo to get directory Size.
268-
if (IsDirectory)
269-
{
270-
Uri fileUri = new Uri(filePath, UriKind.Relative);
271-
StreamResourceInfo streamInfo = Application.GetResourceStream(fileUri);
272-
if (streamInfo != null)
273-
{
274-
this.Size = streamInfo.Stream.Length;
254+
// attempt to get it from the resources
255+
256+
Uri fileUri = new Uri(filePath, UriKind.Relative);
257+
StreamResourceInfo streamInfo = Application.GetResourceStream(fileUri);
258+
if (streamInfo != null)
259+
{
260+
this.Size = streamInfo.Stream.Length;
261+
this.FileName = filePath.Substring(filePath.LastIndexOf("/") + 1);
262+
}
263+
else
264+
{
265+
throw new FileNotFoundException("File doesn't exist");
266+
}
275267
}
276-
}
277-
else
278-
{
279-
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filePath, FileMode.Open, FileAccess.Read, isoFile))
268+
else
280269
{
281-
this.Size = stream.Length;
270+
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filePath, FileMode.Open, FileAccess.Read, isoFile))
271+
{
272+
this.Size = stream.Length;
273+
}
274+
275+
this.FileName = System.IO.Path.GetFileName(filePath);
276+
this.LastModifiedDate = isoFile.GetLastWriteTime(filePath).DateTime.ToString();
282277
}
283278
}
284279

285-
this.FullPath = filePath;
286-
this.FileName = System.IO.Path.GetFileName(filePath);
287-
this.LastModifiedDate = isoFile.GetLastWriteTime(filePath).DateTime.ToString();
288-
}
289280
this.Type = MimeTypeMapper.GetMimeType(this.FileName);
290281
}
291282
}
@@ -917,7 +908,7 @@ public void getFileMetadata(string options)
917908
string filePath = optStings[0];
918909
string callbackId = optStings[1];
919910

920-
if (filePath != null)
911+
if (!string.IsNullOrEmpty(filePath))
921912
{
922913
try
923914
{
@@ -936,6 +927,10 @@ public void getFileMetadata(string options)
936927
}
937928
}
938929
}
930+
else
931+
{
932+
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, NOT_FOUND_ERR), callbackId);
933+
}
939934
}
940935

941936
/// <summary>
@@ -1609,7 +1604,7 @@ private void GetFileOrDirectory(string options, bool getDirectory)
16091604

16101605
// need to make sure the parent exists
16111606
// it is an error to create a directory whose immediate parent does not yet exist
1612-
// see issue: https://issues.apache.org/jira/browse/CB-339
1607+
// see issue: https://issues.apache.org/jira/browse/CB-339
16131608
string[] pathParts = path.Split('/');
16141609
string builtPath = pathParts[0];
16151610
for (int n = 1; n < pathParts.Length - 1; n++)

0 commit comments

Comments
 (0)