2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FMedia_Util.cpp
20 * @brief This file contains the utility APIs of Media namespace.
24 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
27 #include <FIoDirectory.h>
28 #include "FMedia_MediaUtil.h"
29 #include "FBase_StringConverter.h"
31 using namespace Tizen::Base;
32 using namespace Tizen::Io;
34 namespace Tizen { namespace Media {
36 Tizen::Base::ByteBuffer*
37 _MediaUtil::FileToBufferN(const Tizen::Base::String &path, int maxSize)
40 std::unique_ptr<ByteBuffer> pBuf;
44 SysTryReturn(NID_MEDIA, !path.IsEmpty(), null, E_FILE_NOT_FOUND,
45 "[E_FILE_NOT_FOUND] path is empty");
46 SysTryReturn(NID_MEDIA, path.GetLength() > 0 && path.GetLength() <= PATH_MAX, null, E_INVALID_ARG,
47 "[E_INVALID_ARG] Given filePath length is zero or exceeds system limitations.");
48 SysSecureTryReturn(NID_MEDIA, File::IsFileExist(path), null, E_FILE_NOT_FOUND,
49 "[E_FILE_NOT_FOUND] filePath:%ls", path.GetPointer());
51 r = file.Construct(path, L"rb");
52 SysSecureTryReturn(NID_MEDIA, r == E_SUCCESS, null, r,
53 "[%s] file.Construct failed. File name (%ls)", GetErrorMessage(r), path.GetPointer());
55 file.Seek(FILESEEKPOSITION_END, 0);
57 file.Seek(FILESEEKPOSITION_BEGIN, 0);
59 SysSecureTryReturn(NID_MEDIA, size > 0, null, E_DATA_NOT_FOUND,
60 "[E_DATA_NOT_FOUND] File size is 0: %ls", path.GetPointer());
62 pBuf.reset(new (std::nothrow) ByteBuffer());
63 SysTryReturn(NID_MEDIA, pBuf.get() != null, null, E_OUT_OF_MEMORY,
64 "[E_OUT_OF_MEMORY] Failed to create new ByteBuffer.");
66 if (maxSize == 0 || size <= maxSize) // read whole data
68 r = pBuf->Construct(size);
69 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ByteBuffer Construct failed for %d bytes.",
70 GetErrorMessage(r), size);
72 r = file.Read(*pBuf.get());
73 SysSecureTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. File.Read failed: %ls",
74 GetErrorMessage(r), path.GetPointer());
78 r = pBuf->Construct(maxSize);
79 SysTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. ByteBuffer Construct failed for %d bytes.",
80 GetErrorMessage(r), maxSize);
82 r = file.Read(*pBuf.get());
83 SysSecureTryReturn(NID_MEDIA, r == E_SUCCESS, null, r, "[%s] Propagated. File.Read failed: %ls",
84 GetErrorMessage(r), path.GetPointer());
89 SetLastResult(E_SUCCESS);
90 return pBuf.release();
94 _MediaUtil::BufferToFile(const Tizen::Base::ByteBuffer &srcBuf,
95 const Tizen::Base::String &dstPath, bool overwrite)
101 char* tempFileName = null;
102 std::unique_ptr<char[]> pDirectory;
103 std::unique_ptr<char[]> pFileName;
105 if (File::IsFileExist(dstPath) && overwrite == false)
107 return E_FILE_ALREADY_EXIST;
110 String tmpPath = File::GetFileName(dstPath);
112 r = dstPath.SubString(0, dstPath.GetLength() - tmpPath.GetLength() -1, directoryName);
113 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated. Faied to generate tempfile", GetErrorMessage(r));
115 r = dstPath.SubString(dstPath.GetLength() - tmpPath.GetLength(), fileName);
116 SysTryReturn(NID_MEDIA, r == E_SUCCESS, r, r, "[%s] Propagated. Faied to generate tempfile", GetErrorMessage(r));
118 pDirectory.reset(_StringConverter::CopyToCharArrayN(directoryName));
119 SysTryReturn(NID_MEDIA, pDirectory.get() != null, r, r, "[E_OUT_OF_MEMORY] Propagated.");
121 pFileName.reset(_StringConverter::CopyToCharArrayN(fileName));
122 SysTryReturn(NID_MEDIA, pFileName.get() != null, r, r, "[E_OUT_OF_MEMORY] Propagated.");
124 r = Directory::Create(directoryName, true);
125 SysSecureTryReturn(NID_MEDIA, (r == E_SUCCESS || r == E_FILE_ALREADY_EXIST), r, r, "[%s] Propagated. Faied to create directory for %ls",
126 GetErrorMessage(r), directoryName.GetPointer());
128 tempFileName = tempnam(pDirectory.get(), pFileName.get());
129 SysTryReturn(NID_MEDIA, tempFileName != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Propagated. Faied to generate tempfile");
131 tmpPath = String(tempFileName);
133 r = file.Construct(tmpPath, "wb", true);
134 SysSecureTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated. Temp File.Construct failed for %ls",
135 GetErrorMessage(r), tmpPath.GetPointer());
137 r = file.Write(srcBuf);
138 SysSecureTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated. Temp File.Write failed for %ls",
139 GetErrorMessage(r), tmpPath.GetPointer());
141 if (File::IsFileExist(dstPath))
143 r = File::Remove(dstPath);
144 SysSecureTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated. Original File Remove failed for %ls",
145 GetErrorMessage(r), tmpPath.GetPointer());
148 r = file.Move(tmpPath, dstPath);
149 SysSecureTryCatch(NID_MEDIA, r == E_SUCCESS, , r, "[%s] Propagated. File.Move failed for %ls",
150 GetErrorMessage(r), dstPath.GetPointer());
156 if (tempFileName != null)
160 file.Remove(tmpPath);