1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "net/base/upload_data.h" 6 7 #include "base/file_util.h" 8 #include "base/logging.h" 9 10 namespace net { 11 12 uint64 UploadData::GetContentLength() const { 13 uint64 len = 0; 14 std::vector<Element>::const_iterator it = elements_.begin(); 15 for (; it != elements_.end(); ++it) 16 len += (*it).GetContentLength(); 17 return len; 18 } 19 20 uint64 UploadData::Element::GetContentLength() const { 21 if (type_ == TYPE_BYTES) 22 return static_cast<uint64>(bytes_.size()); 23 24 DCHECK(type_ == TYPE_FILE); 25 26 // TODO(darin): This size calculation could be out of sync with the state of 27 // the file when we get around to reading it. We should probably find a way 28 // to lock the file or somehow protect against this error condition. 29 30 int64 length = 0; 31 if (!file_util::GetFileSize(file_path_, &length)) 32 return 0; 33 34 if (file_range_offset_ >= static_cast<uint64>(length)) 35 return 0; // range is beyond eof 36 37 // compensate for the offset and clip file_range_length_ to eof 38 return std::min(length - file_range_offset_, file_range_length_); 39 } 40 41 } // namespace net 42