]> khamidou — GitHub mirrors - electron.git/commitdiff
Make NativeImage work with asar archive
authorCheng Zhao <zcbenz@gmail.com>
Thu, 12 Feb 2015 11:34:21 +0000 (19:34 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 12 Feb 2015 11:34:21 +0000 (19:34 +0800)
atom/common/api/atom_api_native_image.cc
atom/common/asar/asar_util.cc
atom/common/asar/asar_util.h

index 6b873fa7f55a67f929e1c31f2727dbfe14e1e5b0..a57ff221a4247682241f5c2f40308d21167d4f04 100644 (file)
@@ -7,11 +7,11 @@
 #include <string>
 #include <vector>
 
+#include "atom/common/asar/asar_util.h"
 #include "atom/common/native_mate_converters/file_path_converter.h"
 #include "atom/common/native_mate_converters/gfx_converter.h"
 #include "atom/common/native_mate_converters/gurl_converter.h"
 #include "base/base64.h"
-#include "base/files/file_util.h"
 #include "base/strings/string_util.h"
 #include "native_mate/dictionary.h"
 #include "native_mate/object_template_builder.h"
@@ -86,7 +86,7 @@ bool AddImageSkiaRep(gfx::ImageSkia* image,
                      const base::FilePath& path,
                      double scale_factor) {
   std::string file_contents;
-  if (!base::ReadFileToString(path, &file_contents))
+  if (!asar::ReadFileToString(path, &file_contents))
     return false;
 
   const unsigned char* data =
index 241f230c55b38948c20e1c7626a8879b86568f28..7c35ddcc5c2c3031483f5f508263fc0616986305 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "atom/common/asar/archive.h"
 #include "base/files/file_path.h"
+#include "base/files/file_util.h"
 #include "base/lazy_instance.h"
 #include "base/stl_util.h"
 
@@ -57,4 +58,26 @@ bool GetAsarArchivePath(const base::FilePath& full_path,
   return true;
 }
 
+bool ReadFileToString(const base::FilePath& path, std::string* contents) {
+  base::FilePath asar_path, relative_path;
+  if (!GetAsarArchivePath(path, &asar_path, &relative_path))
+    return base::ReadFileToString(path, contents);
+
+  std::shared_ptr<Archive> archive = GetOrCreateAsarArchive(asar_path);
+  if (!archive)
+    return false;
+
+  Archive::FileInfo info;
+  if (!archive->GetFileInfo(relative_path, &info))
+    return false;
+
+  base::File src(asar_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
+  if (!src.IsValid())
+    return false;
+
+  contents->resize(info.size);
+  return static_cast<int>(info.size) == src.Read(
+      info.offset, const_cast<char*>(contents->data()), contents->size());
+}
+
 }  // namespace asar
index 9df55da5f3cd25b96fecbc850ae172f55f73c9cf..b3c60f679b5218f19db017a3f92f5427f983f671 100644 (file)
@@ -23,6 +23,9 @@ bool GetAsarArchivePath(const base::FilePath& full_path,
                         base::FilePath* asar_path,
                         base::FilePath* relative_path);
 
+// Same with base::ReadFileToString but supports asar Archive.
+bool ReadFileToString(const base::FilePath& path, std::string* contents);
+
 }  // namespace asar
 
 #endif  // ATOM_COMMON_ASAR_ASAR_UTIL_H_