/** * Return a FileEntry object. */ void PhoneGapFile::actionGetFile(PhoneGapMessage& message) { lprintfln("@@@ actionGetFile\n"); String callbackID = message.getParam("PhoneGapCallBackId"); String fullPath = message.getArgsField("fullPath"); String path = message.getArgsField("path"); String fullFilePath = fullPath + "/" + path; // Get flags "create" and "exclusive". bool create = false; bool exclusive = false; bool success = message.getJSONParamsOptionsCreateExclusive(create, exclusive); if (!success) { callFileError(callbackID, FILEERROR_NO_MODIFICATION_ALLOWED_ERR); return; } // Create file if requested. if (create) { if (exclusive) { // The file must not exist if "exclusive" is true. if (FileExists(fullFilePath)) { callFileError(callbackID, FILEERROR_PATH_EXISTS_ERR); return; } } if (!FileExists(fullFilePath)) { // Create the file. bool success = FileCreatePath(fullFilePath); if (!success) { callFileError(callbackID, FILEERROR_NO_MODIFICATION_ALLOWED_ERR); return; } } } // Send back FileEntry data. String fileEntry = emitFileEntry( path, fullFilePath); callSuccess( callbackID, fileEntry, "window.localFileSystem._castEntry"); }
void PhoneGapFile::actionReadAsDataURL(PhoneGapMessage& message) { lprintfln("@@@ actionReadAsDataURL\n"); String callbackID = message.getParam("PhoneGapCallBackId"); String fullPath = message.getArgsField("fileName"); String content; int result = FileRead(fullPath, content); if (result < 0) { callFileError(callbackID, FILEERROR_NO_MODIFICATION_ALLOWED_ERR); return; } String base64URL = "\"data:"; base64URL += FileGetMimeType(fullPath); base64URL += ";base64,"; base64URL += PhoneGapMessage::base64Encode(content.c_str()); base64URL += "\""; // Send back the file content. callSuccess(callbackID, base64URL); }
void PhoneGapFile::actionReadAsText(PhoneGapMessage& message) { lprintfln("@@@ actionReadAsText\n"); String callbackID = message.getParam("PhoneGapCallBackId"); String fullPath = message.getArgsField("fileName"); // TODO: Encoding param is not used. This is the requested // encoding of the data send back to PhoneGap. //String encoding = message.getArgsField("encoding"); String content; int result = FileRead(fullPath, content); if (result < 0) { callFileError(callbackID, FILEERROR_NO_MODIFICATION_ALLOWED_ERR); return; } // Send back the file content. callSuccess( callbackID, PhoneGapMessage::JSONStringify(content.c_str())); }
void PhoneGapFile::actionWrite(PhoneGapMessage& message) { lprintfln("@@@ actionWrite\n"); String callbackID = message.getParam("PhoneGapCallBackId"); String fullPath = message.getArgsField("fileName"); String data = message.getArgsField("data"); int position = message.getArgsFieldInt("position"); int result = FileWrite(fullPath, data, position); if (result < 0) { callFileError(callbackID, FILEERROR_NO_MODIFICATION_ALLOWED_ERR); return; } // Send back the new file size. char sizeBuf[32]; sprintf(sizeBuf, "%i", FileGetSize(fullPath)); callSuccess( callbackID, sizeBuf); }
/** * This is only valid for directories. */ void PhoneGapFile::actionRemoveRecursively(PhoneGapMessage& message) { lprintfln("@@@ actionRemoveRecursively\n"); String callbackID = message.getParam("PhoneGapCallBackId"); String path = message.getArgsField("fullPath"); int result = FileDeleteDirectory(path); if (result < 0) { callFileError(callbackID, FILEERROR_NOT_FOUND_ERR); return; } callSuccess(callbackID, "\"ok\""); }
/** * Return a Metadata object. */ void PhoneGapFile::actionGetMetadata(PhoneGapMessage& message) { lprintfln("@@@ actionGetMetadata\n"); String callbackID = message.getParam("PhoneGapCallBackId"); String fullPath = message.getArgsField("fullPath"); String metadata = emitMetadata( FileGetDate(fullPath)); // Note that _castDate is used here. callSuccess( callbackID, metadata, "window.localFileSystem._castDate"); }
void PhoneGapFile::actionResolveLocalFileSystemURI(PhoneGapMessage& message) { lprintfln("@@@ actionResolveLocalFileSystemURI\n"); String callbackID = message.getParam("PhoneGapCallBackId"); String uri = message.getArgsField("uri"); // Get path. const char* pURL = uri.c_str(); lprintfln(">>> pURL: %s", pURL); const char* pPath = strstr(pURL, "file://"); if (NULL == pPath) { callFileError(callbackID, "1000"); return; } if (pURL != pPath) { callFileError(callbackID, FILEERROR_SYNTAX_ERR); return; } // Move to after "file://" pPath += 7; // Check that this is an existing directory. if (!FileIsDirectory(pPath)) { callFileError(callbackID, FILEERROR_NOT_FOUND_ERR); return; } String entry = emitDirectoryEntry( FileGetName(pPath), pPath); callSuccess( callbackID, entry, "window.localFileSystem._castEntry"); }
void PhoneGapFile::actionTruncate(PhoneGapMessage& message) { lprintfln("@@@ actionTruncate\n"); String callbackID = message.getParam("PhoneGapCallBackId"); String fullPath = message.getArgsField("fileName"); int size = message.getArgsFieldInt("size"); int result = FileTruncate(fullPath, size); if (result < 0) { callFileError(callbackID, FILEERROR_NOT_FOUND_ERR); return; } // Send back the result, the new length of the file. char lengthBuf[32]; sprintf(lengthBuf, "%i", result); callSuccess(callbackID, lengthBuf); }
/** * Return a File object. */ void PhoneGapFile::actionGetFileMetadata(PhoneGapMessage& message) { lprintfln("@@@ actionGetFileMetadata\n"); String callbackID = message.getParam("PhoneGapCallBackId"); String fullPath = message.getArgsField("fullPath"); char sizeBuf[64]; sprintf(sizeBuf, "%i", FileGetSize(fullPath)); String file = emitFile( FileGetName(fullPath), fullPath, FileGetMimeType(fullPath), FileGetDate(fullPath), sizeBuf); // Note that _castDate also casts a File object. callSuccess( callbackID, file, "window.localFileSystem._castDate"); }
// mosync://PhoneGap?service=File&action=readEntries&args={"fullPath":"/mnt/sdcard/fob1"}& // PhoneGapCallBackId=File21 void PhoneGapFile::actionReadEntries(PhoneGapMessage& message) { lprintfln("@@@ actionReadEntries\n"); String callbackID = message.getParam("PhoneGapCallBackId"); String path = message.getArgsField("fullPath"); // Open entry array. String entries = "["; char nameBuf[2048]; // Make sure path end with a slash. FileMakeDirectoryPath(path); // Open directory listing. MAHandle list = maFileListStart( path.c_str(), "", MA_FL_SORT_NAME | MA_FL_ORDER_ASCENDING); if (list < 0) { callFileError(callbackID, FILEERROR_NOT_FOUND_ERR); return; } // List all files in this directory. while (true) { // Move to next file. int result = maFileListNext(list, nameBuf, 2048); if (0 == result) { // No more files. break; } if (0 > result) { maFileListClose(list); callFileError(callbackID, FILEERROR_NOT_FOUND_ERR); return; } // Add separating comma if needed. if (entries.size() > 1) { entries += ","; } // Full path to entry. String fullPath = path + nameBuf; // Is this a directory? if ('/' == nameBuf[result - 1]) { // We remove the trailing slash of the directory. String pathWithNoSlash = fullPath.substr(0, fullPath.size() - 1); String entry = emitDirectoryEntry( FileGetName(pathWithNoSlash), pathWithNoSlash); entries += entry; } else { String entry = emitFileEntry( FileGetName(fullPath), fullPath); entries += entry; } } // Close the directory listing. maFileListClose(list); // Close entry array. entries += "]"; // Return result to PhoneGap. callSuccess( callbackID, entries, "window.localFileSystem._castEntries"); }
//I/maWriteLog(20616): @@@ URL: mosync://PhoneGap?service=File&action=moveTo&args={"fullPath":"/mnt/sdcard/hello2.txt","pa //rent":{"isFile":false,"isDirectory":true,"name":"sdcard","fullPath":"/mnt/sdcard","filesystem":null},"newName":"hello3.t //xt"}&PhoneGapCallBackId=File19 void PhoneGapFile::actionCopyMoveHelper(PhoneGapMessage& message, bool move) { String callbackID = message.getParam("PhoneGapCallBackId"); String sourcePath = message.getArgsField("fullPath"); String destinationName = message.getArgsField("newName"); // Get the destination path from the JSON tree. String destinationPath; bool success = message.getJSONParamParentFullPath(destinationPath); if (!success) { callFileError(callbackID, FILEERROR_NO_MODIFICATION_ALLOWED_ERR); return; } // Check that we have the required path names. if ((sourcePath.size() == 0) || (destinationName.size() == 0) || (destinationPath.size() == 0)) { callFileError(callbackID, FILEERROR_NO_MODIFICATION_ALLOWED_ERR); return; } bool isDirectory; // Check that sourcePath exists. if (FileIsDirectory(sourcePath)) { isDirectory = true; } else if (FileExistsHelper(sourcePath)) { isDirectory = false; } else { callFileError(callbackID, FILEERROR_NO_MODIFICATION_ALLOWED_ERR); return; } // Compose the full destination path. String fullDestinationPath = destinationPath + "/" + destinationName; lprintfln(">>>>> sourcePath: %s", sourcePath.c_str()); lprintfln(">>>>> destinationPath: %s", destinationPath.c_str()); lprintfln(">>>>> destinationName: %s", destinationName.c_str()); lprintfln(">>>>> fullDestinationPath: %s", fullDestinationPath.c_str()); int result; if (move) { result = FileMove(sourcePath, fullDestinationPath); } else { result = FileCopy(sourcePath, fullDestinationPath); } if (result < 0) { lprintfln(">>>>> callFileError(callbackID, FILEERROR_NOT_FOUND_ERR);"); callFileError(callbackID, FILEERROR_NOT_FOUND_ERR); return; } // Send back entry data. String entry; if (isDirectory) { entry = emitDirectoryEntry( destinationName, fullDestinationPath); } else { entry = emitFileEntry( destinationName, fullDestinationPath); } callSuccess( callbackID, entry, "window.localFileSystem._castEntry"); }
/** * Return a DirectoryEntry object. */ void PhoneGapFile::actionGetDirectory(PhoneGapMessage& message) { lprintfln("@@@ actionGetDirectory\n"); String callbackID = message.getParam("PhoneGapCallBackId"); String fullPath = message.getArgsField("fullPath"); String path = message.getArgsField("path"); String fullFilePath = fullPath + "/" + path; // Add a trailing slash if not present. MoSync API requires this. if (fullFilePath[fullFilePath.size() - 1] != '/') { fullFilePath += "/"; } // Get flags "create" and "exclusive". bool create = false; bool exclusive = false; bool success = message.getJSONParamsOptionsCreateExclusive(create, exclusive); if (!success) { callFileError(callbackID, FILEERROR_NO_MODIFICATION_ALLOWED_ERR); return; } // Create directory if requested. if (create) { if (exclusive) { // The file must not exist if "exclusive" is true. if (FileExists(fullFilePath)) { callFileError(callbackID, FILEERROR_PATH_EXISTS_ERR); return; } } if (!FileExists(fullFilePath)) { // Create the directory. // TODO: Invoke error if parent directory does not // exist to be compatible with the PhoneGap spec. bool success = FileCreatePath(fullFilePath); if (!success) { callFileError(callbackID, FILEERROR_NO_MODIFICATION_ALLOWED_ERR); return; } } } // Send back DirectoryEntry data. String directoryEntry = emitDirectoryEntry( path, // Remove the trailing slash from the full path. fullFilePath.substr(0, fullFilePath.size() - 1)); callSuccess( callbackID, directoryEntry, "window.localFileSystem._castEntry"); }