This source file includes following definitions.
- HasLabel
- ConvertToResourceEntry
- ConvertResourceEntryToFileInfo
- SetPlatformFileInfoToResourceEntry
#include "chrome/browser/chromeos/drive/resource_entry_conversion.h"
#include <algorithm>
#include <string>
#include "base/logging.h"
#include "base/platform_file.h"
#include "base/time/time.h"
#include "chrome/browser/chromeos/drive/drive.pb.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/drive/drive_api_util.h"
#include "google_apis/drive/gdata_wapi_parser.h"
namespace drive {
namespace {
const char kSharedWithMeLabel[] = "shared-with-me";
const char kSharedLabel[] = "shared";
bool HasLabel(const google_apis::ResourceEntry& entry,
const std::string& label) {
std::vector<std::string>::const_iterator it =
std::find(entry.labels().begin(), entry.labels().end(), label);
return it != entry.labels().end();
}
}
bool ConvertToResourceEntry(const google_apis::ResourceEntry& input,
ResourceEntry* out_entry,
std::string* out_parent_resource_id) {
DCHECK(out_entry);
DCHECK(out_parent_resource_id);
ResourceEntry converted;
converted.set_title(input.title());
converted.set_base_name(util::NormalizeFileName(converted.title()));
converted.set_resource_id(input.resource_id());
std::string parent_resource_id;
const google_apis::Link* parent_link =
input.GetLinkByType(google_apis::Link::LINK_PARENT);
if (parent_link)
parent_resource_id = util::ExtractResourceIdFromUrl(parent_link->href());
converted.set_deleted(input.deleted());
converted.set_shared_with_me(HasLabel(input, kSharedWithMeLabel));
converted.set_shared(HasLabel(input, kSharedLabel));
PlatformFileInfoProto* file_info = converted.mutable_file_info();
file_info->set_last_modified(input.updated_time().ToInternalValue());
file_info->set_last_accessed(input.last_viewed_time().ToInternalValue());
file_info->set_creation_time(input.published_time().ToInternalValue());
if (input.is_file() || input.is_hosted_document()) {
FileSpecificInfo* file_specific_info =
converted.mutable_file_specific_info();
if (input.is_file()) {
file_info->set_size(input.file_size());
file_specific_info->set_md5(input.file_md5());
} else if (input.is_hosted_document()) {
const std::string document_extension = input.GetHostedDocumentExtension();
file_specific_info->set_document_extension(document_extension);
converted.set_base_name(
util::NormalizeFileName(converted.title() + document_extension));
file_info->set_size(0);
}
file_info->set_is_directory(false);
file_specific_info->set_content_mime_type(input.content_mime_type());
file_specific_info->set_is_hosted_document(input.is_hosted_document());
const google_apis::Link* alternate_link =
input.GetLinkByType(google_apis::Link::LINK_ALTERNATE);
if (alternate_link)
file_specific_info->set_alternate_url(alternate_link->href().spec());
const int64 image_width = input.image_width();
if (image_width != -1)
file_specific_info->set_image_width(image_width);
const int64 image_height = input.image_height();
if (image_height != -1)
file_specific_info->set_image_height(image_height);
const int64 image_rotation = input.image_rotation();
if (image_rotation != -1)
file_specific_info->set_image_rotation(image_rotation);
} else if (input.is_folder()) {
file_info->set_is_directory(true);
} else {
if (!input.deleted())
return false;
}
out_entry->Swap(&converted);
swap(*out_parent_resource_id, parent_resource_id);
return true;
}
void ConvertResourceEntryToFileInfo(const ResourceEntry& entry,
base::File::Info* file_info) {
file_info->size = entry.file_info().size();
file_info->is_directory = entry.file_info().is_directory();
file_info->is_symbolic_link = entry.file_info().is_symbolic_link();
file_info->last_modified = base::Time::FromInternalValue(
entry.file_info().last_modified());
file_info->last_accessed = base::Time::FromInternalValue(
entry.file_info().last_accessed());
file_info->creation_time = base::Time::FromInternalValue(
entry.file_info().creation_time());
}
void SetPlatformFileInfoToResourceEntry(const base::File::Info& file_info,
ResourceEntry* entry) {
PlatformFileInfoProto* entry_file_info = entry->mutable_file_info();
entry_file_info->set_size(file_info.size);
entry_file_info->set_is_directory(file_info.is_directory);
entry_file_info->set_is_symbolic_link(file_info.is_symbolic_link);
entry_file_info->set_last_modified(file_info.last_modified.ToInternalValue());
entry_file_info->set_last_accessed(file_info.last_accessed.ToInternalValue());
entry_file_info->set_creation_time(file_info.creation_time.ToInternalValue());
}
}