OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/drive/drive_api_util.h" | 5 #include "chrome/browser/drive/drive_api_util.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 base::StringAppendF( | 114 base::StringAppendF( |
115 &result, | 115 &result, |
116 "%sfullText contains \'%s\'", | 116 "%sfullText contains \'%s\'", |
117 is_exclusion ? "not " : "", | 117 is_exclusion ? "not " : "", |
118 EscapeQueryStringValue(base::UTF16ToUTF8(token)).c_str()); | 118 EscapeQueryStringValue(base::UTF16ToUTF8(token)).c_str()); |
119 } | 119 } |
120 | 120 |
121 return result; | 121 return result; |
122 } | 122 } |
123 | 123 |
124 std::string ExtractResourceIdFromUrl(const GURL& url) { | |
125 return net::UnescapeURLComponent(url.ExtractFileName(), | |
126 net::UnescapeRule::URL_SPECIAL_CHARS); | |
127 } | |
128 | |
129 std::string CanonicalizeResourceId(const std::string& resource_id) { | 124 std::string CanonicalizeResourceId(const std::string& resource_id) { |
130 // If resource ID is in the old WAPI format starting with a prefix like | 125 // If resource ID is in the old WAPI format starting with a prefix like |
131 // "document:", strip it and return the remaining part. | 126 // "document:", strip it and return the remaining part. |
132 std::string stripped_resource_id; | 127 std::string stripped_resource_id; |
133 if (RE2::FullMatch(resource_id, "^[a-z-]+(?::|%3A)([\\w-]+)$", | 128 if (RE2::FullMatch(resource_id, "^[a-z-]+(?::|%3A)([\\w-]+)$", |
134 &stripped_resource_id)) | 129 &stripped_resource_id)) |
135 return stripped_resource_id; | 130 return stripped_resource_id; |
136 return resource_id; | 131 return resource_id; |
137 } | 132 } |
138 | 133 |
139 scoped_ptr<google_apis::ResourceEntry> | 134 scoped_ptr<google_apis::ResourceEntry> |
140 ConvertFileResourceToResourceEntry( | 135 ConvertFileResourceToResourceEntry( |
141 const google_apis::FileResource& file_resource) { | 136 const google_apis::FileResource& file_resource) { |
142 scoped_ptr<google_apis::ResourceEntry> entry(new google_apis::ResourceEntry); | 137 scoped_ptr<google_apis::ResourceEntry> entry(new google_apis::ResourceEntry); |
143 | 138 |
144 // ResourceEntry | 139 // ResourceEntry |
145 entry->set_resource_id(file_resource.file_id()); | 140 entry->set_resource_id(file_resource.file_id()); |
146 entry->set_id(file_resource.file_id()); | |
147 if (file_resource.IsDirectory()) | 141 if (file_resource.IsDirectory()) |
148 entry->set_kind(google_apis::ResourceEntry::ENTRY_KIND_FOLDER); | 142 entry->set_kind(google_apis::ResourceEntry::ENTRY_KIND_FOLDER); |
149 else if (file_resource.IsHostedDocument()) | 143 else if (file_resource.IsHostedDocument()) |
150 entry->set_kind(google_apis::ResourceEntry::ENTRY_KIND_UNKNOWN); | 144 entry->set_kind(google_apis::ResourceEntry::ENTRY_KIND_UNKNOWN); |
151 else | 145 else |
152 entry->set_kind(google_apis::ResourceEntry::ENTRY_KIND_FILE); | 146 entry->set_kind(google_apis::ResourceEntry::ENTRY_KIND_FILE); |
153 entry->set_title(file_resource.title()); | 147 entry->set_title(file_resource.title()); |
154 entry->set_published_time(file_resource.created_date()); | |
155 | |
156 std::vector<std::string> labels; | |
157 if (!file_resource.shared_with_me_date().is_null()) | |
158 labels.push_back("shared-with-me"); | |
159 if (file_resource.shared()) | |
160 labels.push_back("shared"); | |
161 entry->set_labels(labels); | |
162 | |
163 // This should be the url to download the file_resource. | |
164 { | |
165 google_apis::Content content; | |
166 content.set_mime_type(file_resource.mime_type()); | |
167 entry->set_content(content); | |
168 } | |
169 // TODO(kochi): entry->resource_links_ | |
170 | |
171 // For file entries | |
172 entry->set_filename(file_resource.title()); | |
173 entry->set_suggested_filename(file_resource.title()); | |
174 entry->set_file_md5(file_resource.md5_checksum()); | |
175 entry->set_file_size(file_resource.file_size()); | |
176 | 148 |
177 // If file is removed completely, that information is only available in | 149 // If file is removed completely, that information is only available in |
178 // ChangeResource, and is reflected in |removed_|. If file is trashed, the | 150 // ChangeResource, and is reflected in |removed_|. If file is trashed, the |
179 // file entry still exists but with its "trashed" label true. | 151 // file entry still exists but with its "trashed" label true. |
180 entry->set_deleted(file_resource.labels().is_trashed()); | 152 entry->set_deleted(file_resource.labels().is_trashed()); |
181 | 153 |
182 // ImageMediaMetadata | |
183 entry->set_image_width(file_resource.image_media_metadata().width()); | |
184 entry->set_image_height(file_resource.image_media_metadata().height()); | |
185 entry->set_image_rotation(file_resource.image_media_metadata().rotation()); | |
186 | |
187 // CommonMetadata | |
188 entry->set_etag(file_resource.etag()); | |
189 // entry->authors_ | |
190 // entry->links_. | |
191 ScopedVector<google_apis::Link> links; | |
192 for (size_t i = 0; i < file_resource.parents().size(); ++i) { | |
193 google_apis::Link* link = new google_apis::Link; | |
194 link->set_type(google_apis::Link::LINK_PARENT); | |
195 link->set_href(file_resource.parents()[i].parent_link()); | |
196 links.push_back(link); | |
197 } | |
198 if (!file_resource.alternate_link().is_empty()) { | |
199 google_apis::Link* link = new google_apis::Link; | |
200 link->set_type(google_apis::Link::LINK_ALTERNATE); | |
201 link->set_href(file_resource.alternate_link()); | |
202 links.push_back(link); | |
203 } | |
204 entry->set_links(links.Pass()); | |
205 | |
206 // entry->categories_ | |
207 entry->set_updated_time(file_resource.modified_date()); | |
208 entry->set_last_viewed_time(file_resource.last_viewed_by_me_date()); | |
209 | |
210 entry->FillRemainingFields(); | |
211 return entry.Pass(); | 154 return entry.Pass(); |
212 } | 155 } |
213 | 156 |
214 scoped_ptr<google_apis::ResourceEntry> | |
215 ConvertChangeResourceToResourceEntry( | |
216 const google_apis::ChangeResource& change_resource) { | |
217 scoped_ptr<google_apis::ResourceEntry> entry; | |
218 if (change_resource.file()) | |
219 entry = ConvertFileResourceToResourceEntry(*change_resource.file()).Pass(); | |
220 else | |
221 entry.reset(new google_apis::ResourceEntry); | |
222 | |
223 entry->set_resource_id(change_resource.file_id()); | |
224 // If |is_deleted()| returns true, the file is removed from Drive. | |
225 entry->set_removed(change_resource.is_deleted()); | |
226 entry->set_changestamp(change_resource.change_id()); | |
227 entry->set_modification_date(change_resource.modification_date()); | |
228 | |
229 return entry.Pass(); | |
230 } | |
231 | |
232 scoped_ptr<google_apis::ResourceList> | |
233 ConvertFileListToResourceList(const google_apis::FileList& file_list) { | |
234 scoped_ptr<google_apis::ResourceList> feed(new google_apis::ResourceList); | |
235 | |
236 const ScopedVector<google_apis::FileResource>& items = file_list.items(); | |
237 ScopedVector<google_apis::ResourceEntry> entries; | |
238 for (size_t i = 0; i < items.size(); ++i) | |
239 entries.push_back(ConvertFileResourceToResourceEntry(*items[i]).release()); | |
240 feed->set_entries(entries.Pass()); | |
241 | |
242 ScopedVector<google_apis::Link> links; | |
243 if (!file_list.next_link().is_empty()) { | |
244 google_apis::Link* link = new google_apis::Link; | |
245 link->set_type(google_apis::Link::LINK_NEXT); | |
246 link->set_href(file_list.next_link()); | |
247 links.push_back(link); | |
248 } | |
249 feed->set_links(links.Pass()); | |
250 | |
251 return feed.Pass(); | |
252 } | |
253 | |
254 scoped_ptr<google_apis::ResourceList> | |
255 ConvertChangeListToResourceList(const google_apis::ChangeList& change_list) { | |
256 scoped_ptr<google_apis::ResourceList> feed(new google_apis::ResourceList); | |
257 | |
258 const ScopedVector<google_apis::ChangeResource>& items = change_list.items(); | |
259 ScopedVector<google_apis::ResourceEntry> entries; | |
260 for (size_t i = 0; i < items.size(); ++i) { | |
261 entries.push_back( | |
262 ConvertChangeResourceToResourceEntry(*items[i]).release()); | |
263 } | |
264 feed->set_entries(entries.Pass()); | |
265 | |
266 feed->set_largest_changestamp(change_list.largest_change_id()); | |
267 | |
268 ScopedVector<google_apis::Link> links; | |
269 if (!change_list.next_link().is_empty()) { | |
270 google_apis::Link* link = new google_apis::Link; | |
271 link->set_type(google_apis::Link::LINK_NEXT); | |
272 link->set_href(change_list.next_link()); | |
273 links.push_back(link); | |
274 } | |
275 feed->set_links(links.Pass()); | |
276 | |
277 return feed.Pass(); | |
278 } | |
279 | |
280 std::string GetMd5Digest(const base::FilePath& file_path) { | 157 std::string GetMd5Digest(const base::FilePath& file_path) { |
281 const int kBufferSize = 512 * 1024; // 512kB. | 158 const int kBufferSize = 512 * 1024; // 512kB. |
282 | 159 |
283 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 160 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
284 if (!file.IsValid()) | 161 if (!file.IsValid()) |
285 return std::string(); | 162 return std::string(); |
286 | 163 |
287 base::MD5Context context; | 164 base::MD5Context context; |
288 base::MD5Init(&context); | 165 base::MD5Init(&context); |
289 | 166 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 const std::string extension = base::FilePath(path.Extension()).AsUTF8Unsafe(); | 207 const std::string extension = base::FilePath(path.Extension()).AsUTF8Unsafe(); |
331 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { | 208 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { |
332 if (extension == kHostedDocumentKinds[i].extension) | 209 if (extension == kHostedDocumentKinds[i].extension) |
333 return true; | 210 return true; |
334 } | 211 } |
335 return extension == kUnknownHostedDocumentExtension; | 212 return extension == kUnknownHostedDocumentExtension; |
336 } | 213 } |
337 | 214 |
338 } // namespace util | 215 } // namespace util |
339 } // namespace drive | 216 } // namespace drive |
OLD | NEW |