File: file_attachment.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (103 lines) | stat: -rw-r--r-- 3,458 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/nearby_sharing/file_attachment.h"

#include <utility>

#include "base/strings/string_util.h"
#include "chrome/browser/nearby_sharing/share_target.h"
#include "net/base/mime_util.h"

namespace {

FileAttachment::Type FileAttachmentTypeFromMimeType(
    const std::string& mime_type) {
  if (base::StartsWith(mime_type, "image/"))
    return FileAttachment::Type::kImage;

  if (base::StartsWith(mime_type, "video/"))
    return FileAttachment::Type::kVideo;

  if (base::StartsWith(mime_type, "audio/"))
    return FileAttachment::Type::kAudio;

  return FileAttachment::Type::kUnknown;
}

std::string MimeTypeFromPath(const base::FilePath& path) {
  std::string mime_type;
  if (!net::GetWellKnownMimeTypeFromFile(path, &mime_type)) {
    return "application/octet-stream";
  }

  return mime_type;
}

}  // namespace

FileAttachment::FileAttachment(const base::FilePath& file_path)
    : FileAttachment(file_path, file_path.BaseName()) {}

FileAttachment::FileAttachment(const base::FilePath& file_path,
                               const base::FilePath& base_name)
    : Attachment(Attachment::Family::kFile, /*size=*/0),
      file_name_(base_name.AsUTF8Unsafe()),
      mime_type_(MimeTypeFromPath(base_name)),
      type_(FileAttachmentTypeFromMimeType(mime_type_)),
      file_path_(file_path) {}

FileAttachment::FileAttachment(int64_t id,
                               int64_t size,
                               std::string file_name,
                               std::string mime_type,
                               Type type)
    : Attachment(id, Attachment::Family::kFile, size),
      file_name_(std::move(file_name)),
      mime_type_(std::move(mime_type)),
      type_(type) {}

FileAttachment::FileAttachment(const FileAttachment&) = default;

FileAttachment::FileAttachment(FileAttachment&&) = default;

FileAttachment& FileAttachment::operator=(const FileAttachment&) = default;

FileAttachment& FileAttachment::operator=(FileAttachment&&) = default;

FileAttachment::~FileAttachment() = default;

void FileAttachment::MoveToShareTarget(ShareTarget& share_target) {
  share_target.file_attachments.push_back(std::move(*this));
}

const std::string& FileAttachment::GetDescription() const {
  return file_name_;
}

nearby_share::mojom::ShareType FileAttachment::GetShareType() const {
  switch (type()) {
    case FileAttachment::Type::kImage:
      return nearby_share::mojom::ShareType::kImageFile;
    case FileAttachment::Type::kVideo:
      return nearby_share::mojom::ShareType::kVideoFile;
    case FileAttachment::Type::kAudio:
      return nearby_share::mojom::ShareType::kAudioFile;
    default:
      break;
  }

  // Try matching on mime type if the attachment type is unrecognized.
  if (mime_type() == "application/pdf") {
    return nearby_share::mojom::ShareType::kPdfFile;
  } else if (mime_type() == "application/vnd.google-apps.document") {
    return nearby_share::mojom::ShareType::kGoogleDocsFile;
  } else if (mime_type() == "application/vnd.google-apps.spreadsheet") {
    return nearby_share::mojom::ShareType::kGoogleSheetsFile;
  } else if (mime_type() == "application/vnd.google-apps.presentation") {
    return nearby_share::mojom::ShareType::kGoogleSlidesFile;
  } else {
    return nearby_share::mojom::ShareType::kUnknownFile;
  }
}